How to create RowHeadersVisible changed event for DataGridView?

589 views Asked by At

I have a Windows Form, DataGridView and two buttons.
When I will press the button1 it changes a value of RowHeadersVisible to true.
When I will press the button2 it changes a value of RowHeadersVisible to false.

    public Form1()             
    {             
        InitializeComponent();         


        dataGridView1.RowHeadersVisible = false;
    }  

    private void button1_Click(object sender, EventArgs e)
    {
        dataGridView1.RowHeadersVisible = true;
    }
    private void button2_Click(object sender, EventArgs e)
    {
        dataGridView1.RowHeadersVisible = false;
    }   

I cannot find any kind of events about "RowHeadersVisible" value changing in "DataGridView" class. As I mentioned "CellFormatting" event works for this action but it appears often, almost for all kind of action made in datagridview1.
I think we might create a custom event handler in order to make different decisions.
When "RowHeadersVisible" changes the value to false I need to call another function inside "CustomEvent".

    private void CustomEvent(object sender, EventArgs e)
    {
         SomeFunction();
    }

On the other hand "DataGridTableStyle" class has the event "RowHeadersVisibleChanged".
So How to solve this problem?

1

There are 1 answers

2
Hamix On

.NET 4.5, you should get help. Dissolve it in the following way.

Represents the table drawn by the System.Windows.Forms.DataGrid control at run time.

// Instantiate the EventHandler. 
public void AttachRowHeaderVisibleChanged()
{
   myDataGridTableStyle.RowHeadersVisibleChanged += new EventHandler (MyDelegateRowHeadersVisibleChanged);
}

// raise the event when RowHeadersVisible property is changed. 
private void MyDelegateRowHeadersVisibleChanged(object sender, EventArgs e)
{
   string myString = "'RowHeadersVisibleChanged' event raised, Row Headers are";
   if (myDataGridTableStyle.RowHeadersVisible)
      myString += " visible";
   else
      myString += " not visible";

   MessageBox.Show(myString, "RowHeader information");
}

// raise the event when a button is clicked. 
private void myButton_Click(object sender, System.EventArgs e)
{
   if (myDataGridTableStyle.RowHeadersVisible)
      myDataGridTableStyle.RowHeadersVisible = false;
   else
      myDataGridTableStyle.RowHeadersVisible = true;
}