Share via


what is the event that gets fired when data is loaded to the datagrid?

Question

Friday, April 16, 2010 2:50 AM

hi friend,

I have form with data grid and a button, when the button clicked it loads a datatable to the datagrid. What is the event thats get fired when the datatable is loaded to the datagrid. becuase when this data grid load complete i need it to check for certain columns weather meet certain contains in their vlaues...

thanks

All replies (9)

Friday, April 16, 2010 7:09 AM ✅Answered

From the MSDN document, Youcan see DataBindingComplete event of DataGridView will be fired in the following situations.(MSDN:DataGridView.DataBindingComplete )

  • The contents of the DataSource Changed.
  • The value of the DataSource property changed.
  • The value of the DataMember property changed.
  • The value of the BindingContext property changed.

So when you assign DataSource, the first two situations are met. Hence it fires twice.

Reference: DataBindingComplete event is been called twice


Saturday, April 17, 2010 10:48 PM ✅Answered

Hi,

you can put a break point in your event handler and look at the call stack to see what was the order of methods called when the event was raised.
(on a call stack window right click and check option "Show external code" if it's not selected)

From what I've tested ... setting the DataSource in the Form.Load event handler: 

The DataBindingComplete event is raised from the DataSource setter first, internally calling dataConnection.SetDataConnection() (and from there a bunch of methods are called, ending up with OnBindingContextChanged() method which raises BindingContextChanged event and then calls OnDataBindingComplete() which in turn raises DataBindingComplete event.
And the second time it's raised by the OnDataSourceChanged() method, which first raises DataSourceChanged event, and then calls OnDataBindingComplete() which in turn raises DataBindingComplete event.

Is this a desired behavior or not I'm not sure ... looks to me like it's not optimized enough to raise the event only once (if that would even be worth optimizing) ... 
it doesn't get raised twice if you set the DataSource latter on though ... on a button click for instance.

A lot of code there getting executed when you change datasource ... you can further inspect it using .Net Reflector (free tool) if you have the spare time and a strong interest to get to know how things work. In my opinion if you can use it without bugs and performance issues, just use it and move on :)

EDIT: I just tested it some more ... apparently (in my case at least), this was the first control on the form that was bound causing the form BindingContext to be created. That' s why OnBindingContextChanged() was called. When I bind any other control on the form prior to binding DGV, the event is raised only once since the binding context of the form already exists.

 

Best regards,
Vladimir


Friday, April 16, 2010 4:04 AM

I guess it is a windows form application and your a usinf DataGridView control.

You can try DataBindingComplete


Friday, April 16, 2010 4:57 AM

I guess it is a windows form application and your a usinf DataGridView control.

You can try DataBindingComplete

i have used the DataSourceChanged() and it has given me the answer but is there a difference between DataBindingCOmplete and DataSourceChanged() events?

thanks


Friday, April 16, 2010 5:04 AM

DataBindingComplete event is raised when the contents of the data source change or when the value of theDataSource, DataMember, or BindingContextproperty changes.

Handling this event is useful, for example, to programmatically resize rows and columns based on content updates.

The DataSourceChanged event occurs after the DataSourceproperty changes.

Check this example

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewrowcontextmenustripneededeventargs.aspx

 

Thanks,
A.m.a.L
[MVP Visual C#]
Dot Net Goodies
Don't hate the hacker, hate the code

Friday, April 16, 2010 5:20 AM

DataBindingComplete event is raised when the contents of the data source change or when the value of theDataSource,DataMember, orBindingContextproperty changes.

Handling this event is useful, for example, to programmatically resize rows and columns based on content updates.

The DataSourceChanged event occurs after the DataSourceproperty changes.

Check this example

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewrowcontextmenustripneededeventargs.aspx

 

Thanks,
A.m.a.L
[MVP Visual C#]
Dot Net Goodies
Don't hate the hacker, hate the code

so the order of event fire on this situation is:

firstly - DataBindingComplete

secondly - DataSourceChanged ????

thanks

 


Friday, April 16, 2010 5:39 AM | 1 vote

The following example demonstarted, complete event followed by changed

private void Form1_Load(object sender, EventArgs e)
{
  dataGridView1.DataSourceChanged += new EventHandler(dataGridView1_DataSourceChanged);
  dataGridView1.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dataGridView1_DataBindingComplete);

  DataTable dt = new DataTable();
  dt.Columns.Add("1");

  DataRow dr = dt.NewRow();
  dr[0] = "1";

  dt.Rows.Add(dr);

  dataGridView1.DataSource = dt;

}

void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
  MessageBox.Show("complete");
}

void dataGridView1_DataSourceChanged(object sender, EventArgs e)
{
  MessageBox.Show("change");
}
Thanks,
A.m.a.L
[MVP Visual C#]
Dot Net Goodies
Don't hate the hacker, hate the code

Friday, April 16, 2010 6:28 AM

The following example demonstarted, complete event followed by changed

 

private void Form1_Load(object sender, EventArgs e){ dataGridView1.DataSourceChanged += new EventHandler(dataGridView1_DataSourceChanged); dataGridView1.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dataGridView1_DataBindingComplete); DataTable dt = new DataTable(); dt.Columns.Add("1"); DataRow dr = dt.NewRow(); dr[0] = "1"; dt.Rows.Add(dr); dataGridView1.DataSource = dt;}void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e){ MessageBox.Show("complete");}void dataGridView1_DataSourceChanged(object sender, EventArgs e){ MessageBox.Show("change");}

 

Thanks,
A.m.a.L
[MVP Visual C#]
Dot Net Goodies
Don't hate the hacker, hate the code

I have seen this:

when i assign a DataTable to datagrid using the datasource property i see this pattern:

first it calls the DataBindingComplete() event then it calls DataSourceChanged() event then again it calls the DataBindingCompete() evnet. why it calls the DataBindingComplete() event for the second time?

thanks


Saturday, April 17, 2010 1:37 AM

From the MSDN document, Youcan see DataBindingComplete event of DataGridView will be fired in the following situations.(MSDN:DataGridView.DataBindingComplete )

  • The contents of the DataSource Changed.
  • The value of the DataSource property changed.
  • The value of the DataMember property changed.
  • The value of the BindingContext property changed.

So when you assign DataSource, the first two situations are met. Hence it fires twice.

Reference: DataBindingComplete event is been called twice

okay .... accoring to link string.empty was assigned to the datamember of the datagrid so that it should fire the DataBidingComplete() for the second time ... however according to this code:

   try
   {
     Console.WriteLine(dgvProductList.DataMember.Length.ToString());
   }
   catch
   {
    
    throw;
   }      
   
   
   dgvProductList.DataSource = Product.GetAllProducts(); //GetAllProdcuts() returns a datatable
   
   Console.WriteLine("After binding" + dgvProductList.DataMember.Length.ToString());

 

in both print statemetns it prints "0" that means the value of the datamember before the assigment of the datasource to teh datasource property hasnt changed it is string.empty.... but according the the link (second link at the bottom ) given in ramesh reply, datamember changes from null to stirng.empty when i it assigns the datasource tot the datasource property... but it seems like it always set to sting.empty so teh value change of DataMember has not cuased the fire the DataBidingComplete() event for the second time.. so what happneed? what cuased it to fire for the second time...

thanks