Microsoft KB Archive/835405

= FIX: The DataGrid control reports an incorrect row count when a modified column is hidden by scrolling when you are using the .NET Framework 1.1 =

Article ID: 835405

Article Last Modified on 5/21/2007

-

APPLIES TO


 * Microsoft .NET Framework 1.1

-





SYMPTOMS
When you scroll the column of a DataGrid control out of the visible area after you modify the column and then you access the count of the rows that are modified, you receive an incorrect row count.



CAUSE
This problem occurs because the Leave event of the DataGrid control typically causes the EndCurrentEdit method to update the data source. However, when you modify a column and then you scroll that column out of view, the Leave event does not cause the EndCurrentEdit method to be called. Therefore, the changes that are made to the DataGrid control are not updated in the data source.



RESOLUTION
To resolve this problem, obtain the latest service pack for the Microsoft .NET Framework 1.1. To download the latest service pack visit the following Microsoft Developer Network (MSDN) Web site:

http://www.microsoft.com/downloads/details.aspx?FamilyId=A8F5654F-088E-40B2-BBDB-A83353618B38



STATUS
Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the &quot;Applies to&quot; section. This problem was first corrected in Microsoft .NET Framework 1.1 Service Pack 1.



Create a Windows application

 * 1) Create a Windows application that is named MyApp by using Microsoft Visual Basic .NET 2003. By default, a form that is named Form1 and a file that is named Form1.vb are created.

Note If you use Microsoft Visual C# .NET 2003, a file that is named Form1.cs is created.
 * 1) Add a DataGrid control to Form1.
 * 2) Add two Button controls to Form1. By default, the Button1 Button control and the Button2 Button control are created.
 * 3) On the View menu, click Properties Window.
 * 4) In the Properties window of the Button1 Button control, set the Text property to Apply.
 * 5) In the Properties window of the Button2 Button control, set the Text property to Load.
 * 6) In the Properties window of the DataGrid1 DataGrid control, set the Size property to 200, 150.

Add code to the form
 In Solution Explorer, right-click Form1.vb, and then click View Code.  In the Code view of the Form1.vb file, add the following code to the declarations section of the Form1 class:

Visual Basic .NET code Private dataSet1 As System.Data.DataSet Private TestTable As System.Data.DataTable Private dataColumn1 As System.Data.DataColumn Private dataColumn2 As System.Data.DataColumn Private dataColumn3 As System.Data.DataColumn Private dataColumn4 As System.Data.DataColumn Private dataColumn5 As System.Data.DataColumn Private dataColumn6 As System.Data.DataColumn Private dataColumn7 As System.Data.DataColumn Private dataColumn8 As System.Data.DataColumn Visual C# .NET code private System.Data.DataSet dataSet1; private System.Data.DataTable TestTable; private System.Data.DataColumn dataColumn1; private System.Data.DataColumn dataColumn2; private System.Data.DataColumn dataColumn3; private System.Data.DataColumn dataColumn4; private System.Data.DataColumn dataColumn5; private System.Data.DataColumn dataColumn6; private System.Data.DataColumn dataColumn7; private System.Data.DataColumn dataColumn8;   In Design view, double-click the Form1 form, and then add the following code to the Form1_Load procedure:

Visual Basic .NET code Me.dataSet1 = New System.Data.DataSet Me.TestTable = New System.Data.DataTable Me.dataSet1.DataSetName = &quot;NewDataSet&quot; Me.dataSet1.Locale = New System.Globalization.CultureInfo(&quot;en-US&quot;) 'Add new columns to the data table. Me.dataColumn1 = New System.Data.DataColumn Me.dataColumn2 = New System.Data.DataColumn Me.dataColumn3 = New System.Data.DataColumn Me.dataColumn4 = New System.Data.DataColumn Me.dataColumn5 = New System.Data.DataColumn Me.dataColumn6 = New System.Data.DataColumn Me.dataColumn7 = New System.Data.DataColumn Me.dataColumn8 = New System.Data.DataColumn Me.dataSet1.Tables.AddRange(New System.Data.DataTable {Me.TestTable}) Me.TestTable.Columns.AddRange(New System.Data.DataColumn {Me.dataColumn1, _ Me.dataColumn2, Me.dataColumn3, Me.dataColumn4, Me.dataColumn5, Me.dataColumn6, _ Me.dataColumn7, Me.dataColumn8}) Me.TestTable.TableName = &quot;TestTable&quot; 'Assign names to the columns of the data table. Me.dataColumn1.ColumnName = &quot;ID&quot; Me.dataColumn1.DataType = System.Type.GetType(&quot;System.Int32&quot;) Me.dataColumn2.ColumnName = &quot;Column2&quot; Me.dataColumn3.ColumnName = &quot;Column3&quot; Me.dataColumn4.ColumnName = &quot;Column4&quot; Me.dataColumn5.ColumnName = &quot;Column5&quot; Me.dataColumn6.ColumnName = &quot;Column6&quot; Me.dataColumn7.ColumnName = &quot;Column7&quot; Me.dataColumn8.ColumnName = &quot;Column8&quot; 'Add rows to the data table. Dim values As Object = {} Me.TestTable.Rows.Add(New Object {1, &quot;One&quot;, &quot;&quot;}) Me.TestTable.Rows.Add(New Object {2, &quot;Two&quot;, &quot;&quot;}) Me.TestTable.Rows.Add(New Object {3, &quot;Three&quot;, &quot;&quot;}) Me.TestTable.Rows.Add(New Object {4, &quot;Four&quot;, &quot;&quot;}) Me.TestTable.Rows.Add(New Object {5, &quot;Five&quot;, &quot;&quot;}) Me.TestTable.AcceptChanges Visual C# .NET code this.dataSet1 = new System.Data.DataSet; this.TestTable = new System.Data.DataTable; this.dataSet1.DataSetName = &quot;NewDataSet&quot;; this.dataSet1.Locale = new System.Globalization.CultureInfo(&quot;en-US&quot;); //Add new columns to the data table. this.dataColumn1 = new System.Data.DataColumn; this.dataColumn2 = new System.Data.DataColumn; this.dataColumn3 = new System.Data.DataColumn; this.dataColumn4 = new System.Data.DataColumn; this.dataColumn5 = new System.Data.DataColumn; this.dataColumn6 = new System.Data.DataColumn; this.dataColumn7 = new System.Data.DataColumn; this.dataColumn8 = new System.Data.DataColumn; this.dataSet1.Tables.AddRange(new System.Data.DataTable[] {this.TestTable}); this.TestTable.Columns.AddRange(new System.Data.DataColumn[]{                                                                this.dataColumn1,                                                               this.dataColumn2,                                                               this.dataColumn3,                                                               this.dataColumn4,                                                               this.dataColumn5,                                                               this.dataColumn6,                                                               this.dataColumn7,                                                               this.dataColumn8                                                             }); this.TestTable.TableName = &quot;TestTable&quot;; //Assign names to the columns of the data table. this.dataColumn1.ColumnName = &quot;ID&quot;; this.dataColumn1.DataType = typeof(int); this.dataColumn2.ColumnName = &quot;Column2&quot;; this.dataColumn3.ColumnName = &quot;Column3&quot;; this.dataColumn4.ColumnName = &quot;Column4&quot;; this.dataColumn5.ColumnName = &quot;Column5&quot;; this.dataColumn6.ColumnName = &quot;Column6&quot;; this.dataColumn7.ColumnName = &quot;Column7&quot;; this.dataColumn8.ColumnName = &quot;Column8&quot;; //Add rows to the data table. this.TestTable.Rows.Add(new object[]{1, &quot;One&quot;, null}); this.TestTable.Rows.Add(new object[]{2, &quot;Two&quot;, null}); this.TestTable.Rows.Add(new object[]{3, &quot;Three&quot;, null}); this.TestTable.Rows.Add(new object[]{4, &quot;Four&quot;, null}); this.TestTable.Rows.Add(new object[]{5, &quot;Five&quot;, null}); this.TestTable.AcceptChanges;   In Design view of the Form1 form, double-click the Button1 Button control, and then add the following code to the Button1_Click procedure:

Visual Basic .NET code Dim sourceDataView As New DataView((CType(Me.DataGrid1.DataSource, DataView).Table), _ &quot;&quot;, &quot;&quot;, DataViewRowState.ModifiedCurrent) 'Displays the count of the rows that are modified. MessageBox.Show(sourceDataView.Count.ToString) If (sourceDataView.Count <> 0) Then sourceDataView.Table.AcceptChanges End If Visual C# .NET code DataView sourceDataView = new DataView(((DataView)this.dataGrid1.DataSource).Table, &quot;&quot;, &quot;&quot;, DataViewRowState.ModifiedCurrent); //Displays the count of the rows that are modified. MessageBox.Show(sourceDataView.Count.ToString); if(sourceDataView.Count != 0) {  sourceDataView.Table.AcceptChanges; }   In Design view of the Form1 form, double-click the Button2 Button control, and then add the following code to the Button2_Click procedure:

Visual Basic .NET code Dim dview As New DataView(Me.dataSet1.Tables(&quot;TestTable&quot;)) 'Displays the data of the data table in the DataGrid control. Display(dview, &quot;&quot;) Visual C# .NET code //Displays the data of the data table in the DataGrid control. this.Display(new DataView(this.dataSet1.Tables[&quot;TestTable&quot;]),null); </li>  Add the following code after the Button2_Click procedure:

Visual Basic .NET code Public Sub Display(ByVal dataSource As Object, ByVal dataMember As String) 'Sets the data source of the DataGrid control. Me.DataGrid1.DataSource = dataSource Me.DataGrid1.DataMember = dataMember End Sub Visual C# .NET code public void Display(object dataSource, string dataMember) {  //Sets the data source of the DataGrid control. this.dataGrid1.DataSource = dataSource; this.dataGrid1.DataMember = dataMember; } </li></ol>

Build the application

 * 1) On the Build menu, click Build Solution.
 * 2) On the Debug menu, click Start.
 * 3) On the Form1 form, click Load to load the data to the DataGrid control.
 * 4) Modify the value in the second column of any row in the DataGrid control.
 * 5) Without moving the row pointer from the second column of the DataGrid control, drag the horizontal scroll bar to the right by using the mouse pointer until the second column moves out of the visible area.
 * 6) Click Apply to display the count of the modified rows. Notice that the count is 0.
 * 7) Move the horizontal scroll bar back to the original position, and then click Apply again. Notice that the count is 1.

<div class="references_section">