views:

744

answers:

2

Hi,

I am creating a windows application in c#,

I have 1 Datagridview and after I set it's DataSource it should have 3 Rows.

I have attached 2 Event Habdlers to the Datagrdview

void dgvProductList_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)<br>
{
     //this gets called 4 times
}

private void dgvProductList_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{            
    //gets called 2 times.
}

Why does this Happen and how would I fix it?

Thanks In Advance...

A: 

I believe it depends on your other methods, where you're actually setting the DataSource. It seems to me like you're either setting the DataSource twice or calling DataBind twice, and on each DataBind you're actually getting two rows into the DataGridView, thus the 4 total.

Some additional code might help...

Some things to try: search for DataBind(), DataSource(), etc in your code and make sure it's only being called in the appropriate places.

Additionally, it's not necessarily bad for the DataBind to be called twice, assuming something changes between the databinds... some change of state... really hard to say without background.

DNeoMatrix
+1  A: 

From msdn online DataBindingComplete Event

This event is raised when the contents of the data source change or when the value of the DataSource, DataMember, or BindingContext property changes.

E.g. If you are assigning Datasource, and then adding 3 rows --> 4 times event fired

Peter Gfader