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.
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.
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:
Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section. This problem was first corrected in Microsoft .NET Framework 1.1 Service Pack 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.
Add a DataGrid control to
Form1.
Add two Button controls to Form1. By default, the
Button1 Button control and the Button2 Button
control are created.
On the View menu, click Properties
Window.
In the Properties window of the
Button1 Button control, set the Text property
to Apply.
In the Properties window of the
Button2 Button control, set the Text property
to Load.
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
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 = "NewDataSet"
Me.dataSet1.Locale = New System.Globalization.CultureInfo("en-US")
'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 = "TestTable"
'Assign names to the columns of the data table.
Me.dataColumn1.ColumnName = "ID"
Me.dataColumn1.DataType = System.Type.GetType("System.Int32")
Me.dataColumn2.ColumnName = "Column2"
Me.dataColumn3.ColumnName = "Column3"
Me.dataColumn4.ColumnName = "Column4"
Me.dataColumn5.ColumnName = "Column5"
Me.dataColumn6.ColumnName = "Column6"
Me.dataColumn7.ColumnName = "Column7"
Me.dataColumn8.ColumnName = "Column8"
'Add rows to the data table.
Dim values() As Object = {}
Me.TestTable.Rows.Add(New Object() {1, "One", ""})
Me.TestTable.Rows.Add(New Object() {2, "Two", ""})
Me.TestTable.Rows.Add(New Object() {3, "Three", ""})
Me.TestTable.Rows.Add(New Object() {4, "Four", ""})
Me.TestTable.Rows.Add(New Object() {5, "Five", ""})
Me.TestTable.AcceptChanges()
Visual C# .NET code
this.dataSet1 = new System.Data.DataSet();
this.TestTable = new System.Data.DataTable();
this.dataSet1.DataSetName = "NewDataSet";
this.dataSet1.Locale = new System.Globalization.CultureInfo("en-US");
//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 = "TestTable";
//Assign names to the columns of the data table.
this.dataColumn1.ColumnName = "ID";
this.dataColumn1.DataType = typeof(int);
this.dataColumn2.ColumnName = "Column2";
this.dataColumn3.ColumnName = "Column3";
this.dataColumn4.ColumnName = "Column4";
this.dataColumn5.ColumnName = "Column5";
this.dataColumn6.ColumnName = "Column6";
this.dataColumn7.ColumnName = "Column7";
this.dataColumn8.ColumnName = "Column8";
//Add rows to the data table.
this.TestTable.Rows.Add(new object[]{1, "One", null});
this.TestTable.Rows.Add(new object[]{2, "Two", null});
this.TestTable.Rows.Add(new object[]{3, "Three", null});
this.TestTable.Rows.Add(new object[]{4, "Four", null});
this.TestTable.Rows.Add(new object[]{5, "Five", 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), _
"", "", 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, "",
"", 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("TestTable"))
'Displays the data of the data table in the DataGrid control.
Display(dview, "")
Visual C# .NET code
//Displays the data of the data table in the DataGrid control.
this.Display(new DataView(this.dataSet1.Tables["TestTable"]),null);
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;
}
Build the application
On the Build menu, click Build
Solution.
On the Debug menu, click
Start.
On the Form1 form, click Load to load the
data to the DataGrid control.
Modify the value in the second column of any row in the
DataGrid control.
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.
Click Apply to display the count of the
modified rows. Notice that the count is 0.
Move the horizontal scroll bar back to the original
position, and then click Apply again. Notice that the count is 1.