Hi Patrik,
Sorry for the late response, I am out of office at home yesterday.
I finally contacted one development lead of .Net Winform team, and below is
his comment:
During databinding, the "new row" at the bottom gets added and removed
several times - this adds some instances of the RowsAdded events. You
could prevent this by setting
this.dataGridView1.AllowUserToAddRows = false;
in the designer.cs file.
When you use this code:
private void Form1_Load(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("Form1 Load start");
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("value", typeof(int)));
dt.Columns.Add(new DataColumn("name", typeof(string)));
for (int i = 0; i < 1; i++)
{
DataRow dr = dt.NewRow();
dr["value"] = i;
dr["name"] = "item" + i.ToString();
dt.Rows.Add(dr);
}
System.Diagnostics.Debug.WriteLine("Form1 Load - setting
DataSource");
this.dataGridView1.DataSource = dt;
System.Diagnostics.Debug.WriteLine("Form1 Load - setting
AllowUserToAddRows");
this.dataGridView1.AllowUserToAddRows = true;
System.Diagnostics.Debug.WriteLine("Form1 Load done");
}
private void dataGridView1_RowsAdded(object sender,
DataGridViewRowsAddedEventArgs e)
{
System.Diagnostics.Debug.WriteLine("Rows added: e.RowIndex=" +
e.RowIndex.ToString() + ", e.RowCount=" + e.RowCount.ToString());
}
private void dataGridView1_DataBindingComplete(object sender,
DataGridViewBindingCompleteEventArgs e)
{
System.Diagnostics.Debug.WriteLine("DataBindingComplete event
triggered");
}
private void dataGridView1_RowsRemoved(object sender,
DataGridViewRowsRemovedEventArgs e)
{
System.Diagnostics.Debug.WriteLine("Row removed: " +
e.RowIndex.ToString());
}
private void dataGridView1_DataSourceChanged(object sender,
EventArgs e)
{
System.Diagnostics.Debug.WriteLine("DataSourceChanged event
triggered");
}
private void dataGridView1_DataMemberChanged(object sender,
EventArgs e)
{
System.Diagnostics.Debug.WriteLine("DataMemberChanged event
triggered");
}
You get this output:
Form1 Load start
Form1 Load - setting DataSource
Rows added: e.RowIndex=0, e.RowCount=1
DataBindingComplete event triggered
Row removed: 0
Rows added: e.RowIndex=0, e.RowCount=1
DataSourceChanged event triggered
DataBindingComplete event triggered
Form1 Load - setting AllowUserToAddRows
Rows added: e.RowIndex=1, e.RowCount=1
Form1 Load done
First DataBindingComplete event raised:
>
WindowsApplication13.exe!WindowsApplication13.Form1.dataGridView1_DataBindin
gComplete(object sender = {System.Windows.Forms.DataGridView},
System.Windows.Forms.DataGridViewBindingCompleteEventArgs e =
{System.Windows.Forms.DataGridViewBindingCompleteEventArgs}) Line 49 C#
System.Windows.Forms.dll!System.Windows.Forms.DataGridView.OnDataBindingComp
lete(System.Windows.Forms.DataGridViewBindingCompleteEventArgs e =
{System.Windows.Forms.DataGridViewBindingCompleteEventArgs}) Line 14964 +
0xd bytes C#
System.Windows.Forms.dll!System.Windows.Forms.DataGridView.OnDataBindingComp
lete(System.ComponentModel.ListChangedType listChangedType = Reset) Line
14955 + 0x22 bytes C#
System.Windows.Forms.dll!System.Windows.Forms.DataGridView.OnBindingContextC
hanged(System.EventArgs e = {System.EventArgs}) Line 11563 + 0xa bytes C#
System.Windows.Forms.dll!System.Windows.Forms.Control.OnParentBindingContext
Changed(System.EventArgs e = {System.EventArgs}) Line 8105 + 0xc bytes C#
System.Windows.Forms.dll!System.Windows.Forms.Control.OnBindingContextChange
d(System.EventArgs e = {System.EventArgs}) Line 7829 + 0x1d bytes C#
System.Windows.Forms.dll!System.Windows.Forms.Control.BindingContextInternal
.set(System.Windows.Forms.BindingContext value =
{System.Windows.Forms.BindingContext}) Line 1265 + 0x10 bytes C#
System.Windows.Forms.dll!System.Windows.Forms.Control.BindingContext.set(Sys
tem.Windows.Forms.BindingContext value =
{System.Windows.Forms.BindingContext}) Line 1284 + 0x9 bytes C#
System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.BindingContex
t.set(System.Windows.Forms.BindingContext value =
{System.Windows.Forms.BindingContext}) Line 255 + 0xa bytes C#
System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.BindingContex
t.get() Line 250 + 0xc bytes C#
System.Windows.Forms.dll!System.Windows.Forms.Control.BindingContextInternal
.get() Line 1248 + 0xa bytes C#
System.Windows.Forms.dll!System.Windows.Forms.Control.BindingContext.get()
Line 1281 + 0x7 bytes C#
System.Windows.Forms.dll!System.Windows.Forms.DataGridView.DataGridViewDataC
onnection.SetDataConnection(object dataSource = {}, string dataMember = "")
Line 1147 + 0xe bytes C#
System.Windows.Forms.dll!System.Windows.Forms.DataGridView.DataSource.set(ob
ject value = {}) Line 2418 + 0x28 bytes C#
WindowsApplication13.exe!WindowsApplication13.Form1.Form1_Load(object
sender = {WindowsApplication13.Form1, Text: Form1}, System.EventArgs e =
{System.EventArgs}) Line 36 + 0x12 bytes C#
Second DataBindingComplete event raised:
>
WindowsApplication13.exe!WindowsApplication13.Form1.dataGridView1_DataBindin
gComplete(object sender = {System.Windows.Forms.DataGridView},
System.Windows.Forms.DataGridViewBindingCompleteEventArgs e =
{System.Windows.Forms.DataGridViewBindingCompleteEventArgs}) Line 49 C#
System.Windows.Forms.dll!System.Windows.Forms.DataGridView.OnDataBindingComp
lete(System.Windows.Forms.DataGridViewBindingCompleteEventArgs e =
{System.Windows.Forms.DataGridViewBindingCompleteEventArgs}) Line 14964 +
0xd bytes C#
System.Windows.Forms.dll!System.Windows.Forms.DataGridView.OnDataBindingComp
lete(System.ComponentModel.ListChangedType listChangedType = Reset) Line
14955 + 0x22 bytes C#
System.Windows.Forms.dll!System.Windows.Forms.DataGridView.OnDataSourceChang
ed(System.EventArgs e = {System.EventArgs}) Line 15276 + 0x9 bytes C#
System.Windows.Forms.dll!System.Windows.Forms.DataGridView.DataSource.set(ob
ject value = {}) Line 2433 + 0x10 bytes C#
WindowsApplication13.exe!WindowsApplication13.Form1.Form1_Load(object
sender = {WindowsApplication13.Form1, Text: Form1}, System.EventArgs e =
{System.EventArgs}) Line 36 + 0x12 bytes C#
The customer may want to use the DataSourceChanged or DataBindingComplete
events initially and then RowsAdded / RowRemoved later on, using some sort
of internal flag.
When 5 rows are added instead of 1, you get this output:
Form1 Load begining
Form1 Load - setting DataSource
Rows added: e.RowIndex=0, e.RowCount=1
Rows added: e.RowIndex=1, e.RowCount=4
DataBindingComplete event triggered
Row removed: 0
Rows added: e.RowIndex=0, e.RowCount=1
Rows added: e.RowIndex=1, e.RowCount=4
DataSourceChanged event triggered
DataBindingComplete event triggered
Form1 Load - setting AllowUserToAddRows
Rows added: e.RowIndex=5, e.RowCount=1
Form1 Load done
So you may use DataSourceChanged event to enumerate all the rows and
generate the combobox in each row during initial form_loading. In this
period, you may suppress the using of RowAdded event through an internal
flag. After setting AllowUserToAddRows property to true and last RowAdded
event goes through, you may toggle that internal flag to use RowAdded event
in the life time of the application. I hope this logic makes sense to you.
Thanks.
Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Patrik - 26 Feb 2007 07:01 GMT
Thank you Jefferey for your reply!
> Hi Patrik,
>
[quoted text clipped - 217 lines]
> This posting is provided "AS IS" with no warranties, and confers no
> rights.