FIX: Data is not sorted correctly on a DataGrid control in a Windows application in the .NET Framework 1.1

Article translations Article translations
Article ID: 828099 - View products that this article applies to.
Notice
Expand all | Collapse all

On This Page

SYMPTOMS

When you add a DataGrid control to a Windows application that is created by using Microsoft Visual Studio .NET, and then you set the DataSource property of the DataGrid control, you may notice that the data is not sorted correctly after you delete a data record from the data source.

RESOLUTION

Software update information

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 Web site:
http://www.microsoft.com/downloads/details.aspx?FamilyId=A8F5654F-088E-40B2-BBDB-A83353618B38

WORKAROUND

To work around this problem, follow these steps:
  1. Locate the Bind method in the Form1.cs file:
    frm.Controls.Add(dataGrid); 
    		dataGrid.DataSource = dv;
  2. Replace the code that you located in step 1 with the following code:
    dataGrid.DataSource = dv;
    		frm.Controls.Add(dataGrid);
  3. Rebuild the application, and then run the application.

    You will not notice the behavior that is mentioned in the "Symptoms" section.

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 the Microsoft .NET Framework 1.1 Service Pack 1.

MORE INFORMATION

The code in the "Steps to reproduce the behavior" section demonstrates this problem. This application contains one dataset that contains one data table. The data table is populated with five rows inside the code. Then, the data is sorted on one of the columns that is of type Integer in the data table. After this, the code deletes some rows from the data table.

Steps to reproduce the behavior

  1. Start Visual Studio .NET.
  2. On the File menu, point to New, and then click Project. The New Project dialog box appears.
  3. Under Project Types, click Visual C# Projects.
  4. Under Templates, click Windows Application, and then click OK. By default, a form that is named Form1 is created.
  5. In the Solution Explorer window, right-click the Form1.cs file, and then click View Code.
  6. Replace the existing code with the following code:
    using System;
    using System.Data;
    using System.Windows.Forms;
    
    class TestForm : Form 
    {
    	public TestForm()
    	{
    		InitializeComponent();
    	}
    
    	private void InitializeComponent()
    	{
    		DataSet ds = new DataSet();
    		DataTable dt = ds.Tables.Add("Customers");
    		DataColumn c = dt.Columns.Add("Id", typeof(int));
    		dt.Columns.Add("Name", typeof(string));
    		dt.Columns.Add("Related", typeof(string));
    		DataRow dr= dt.NewRow();
    		DataRow dr1= dt.NewRow();
    		DataRow dr2= dt.NewRow();
    		DataRow dr3= dt.NewRow();
    		DataRow dr4= dt.NewRow();
    		//
    		dr["Id"]=1;
    		dr["Name"]="A";
    		dr["Related"]="";
    		dt.Rows.Add(dr);
    		//
    		dr1["Id"]=2;
    		dr1["Name"]="B";
    		dr1["Related"]="A";
    		dt.Rows.Add(dr1);
    		//
    		dr2["Id"]=3;
    		dr2["Name"]="C";
    		dr2["Related"]="";
    		dt.Rows.Add(dr2);
    		//
    		dr3["Id"]=4;
    		dr3["Name"]="D";
    		dr3["Related"]="A";
    		dt.Rows.Add(dr3);
    		//
    		dr4["Id"]=5;
    		dr4["Name"]="E";
    		dr4["Related"]="";
    		dt.Rows.Add(dr4);
    		//	
    		DataView dv = new DataView(ds.Tables["Customers"]);
    		dv.Sort = "Id ASC";
    		//
    		Bind(dv);
    		//Create another view with the rowFilter property that is set 
      //to the name value of the row that will be deleted.
    		DataView dv1 = new DataView(ds.Tables["Customers"]);
    		dv1.RowFilter = String.Format("Related = '{0}'", dv[0]["Name"]);
    		for (int i = 0; i < dv1.Count; i++) 
    		{
    			dv1[i].Delete();
    		}
    
    		dv[0].Delete();
    
    		for (int i = 0; i < dv.Count; i++) 
    		{
    			dv[i][0] = i + 1;
    		} 
    	}
    
    	public void Bind(DataView dv)
    	{
    		Form frm = this;
    		DataGrid dataGrid = new DataGrid();
    		dataGrid.Dock = DockStyle.Fill;
    		dataGrid.Size = frm.ClientSize;
    		frm.Controls.Add(dataGrid); 
    		dataGrid.DataSource = dv;
    	}
    
    	public static void Main()
    	{
    		try 
    		{
    			Application.Run(new TestForm());
    		} 
    		catch (Exception exc) 
    		{
    			MessageBox.Show(exc.ToString());
    		} 
    	}
    }
  7. On the Build menu, click Build Solution.
  8. On the Debug menu, click Start. You notice the behavior that is mentioned in the "Symptoms" section.
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
For more information, visit the following Microsoft Developer Network (MSDN) Web site:
Sorting and Filtering Data Using a DataView
http://msdn2.microsoft.com/en-us/library/13wb36xf(vs.71).aspx

Properties

Article ID: 828099 - Last Review: May 21, 2007 - Revision: 1.3
APPLIES TO
  • Microsoft .NET Framework 1.1
Keywords: 
kbhotfixserver kbqfe kbbug kbnetframe110sp1fix kbdatabinding kbwindowsforms kbtable kbdll kbcontrol kbqfe kbnetframe110presp1fix kbfix KB828099

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com