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 (835405)



The information in this article 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:

STATUS

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.

MORE INFORMATION

Steps to reproduce the behavior

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.
  2. Add a DataGrid control to Form1.
  3. Add two Button controls to Form1. By default, the Button1 Button control and the Button2 Button control are created.
  4. On the View menu, click Properties Window.
  5. In the Properties window of the Button1 Button control, set the Text property to Apply.
  6. In the Properties window of the Button2 Button control, set the Text property to Load.
  7. In the Properties window of the DataGrid1 DataGrid control, set the Size property to 200, 150.

Add code to the form

  1. In Solution Explorer, right-click Form1.vb, and then click View Code.
  2. 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;
  3. 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();
  4. 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();
    }
  5. 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);
    
  6. 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

  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.

REFERENCES

For more information, visit the following MSDN Web sites: For additional information, click the following article number to view the article in the Microsoft Knowledge Base:

824684 Description of the standard terminology that is used to describe Microsoft software updates


Modification Type:MinorLast Reviewed:10/25/2005
Keywords:kbHotfixServer kbQFE kbNetFrame110sp1fix kbNetFrame110preSP1fix kbForms kbControl kbDataview kbQFE kbfix kbBug KB835405 kbAudDeveloper