Microsoft KB Archive/828099

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

Article ID: 828099

Article Last Modified on 5/21/2007

-

APPLIES TO


 * Microsoft .NET Framework 1.1

-



Notice


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.



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:   Locate the Bind method in the Form1.cs file: frm.Controls.Add(dataGrid); dataGrid.DataSource = dv;   Replace the code that you located in step 1 with the following code: dataGrid.DataSource = dv; frm.Controls.Add(dataGrid);  Rebuild the application, and then run the application.

You will not notice the behavior that is mentioned in the &quot;Symptoms&quot; section.



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



MORE INFORMATION
The code in the &quot;Steps to reproduce the behavior&quot; 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
 Start Visual Studio .NET.</li> On the File menu, point to New, and then click Project. The New Project dialog box appears.</li> Under Project Types, click Visual C# Projects.</li> Under Templates, click Windows Application, and then click OK. By default, a form that is named Form1 is created.</li> In the Solution Explorer window, right-click the Form1.cs file, and then click View Code.</li>  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(&quot;Customers&quot;); DataColumn c = dt.Columns.Add(&quot;Id&quot;, typeof(int)); dt.Columns.Add(&quot;Name&quot;, typeof(string)); dt.Columns.Add(&quot;Related&quot;, typeof(string)); DataRow dr= dt.NewRow; DataRow dr1= dt.NewRow; DataRow dr2= dt.NewRow; DataRow dr3= dt.NewRow; DataRow dr4= dt.NewRow; //       dr[&quot;Id&quot;]=1; dr[&quot;Name&quot;]=&quot;A&quot;; dr[&quot;Related&quot;]=&quot;&quot;; dt.Rows.Add(dr); //       dr1[&quot;Id&quot;]=2; dr1[&quot;Name&quot;]=&quot;B&quot;; dr1[&quot;Related&quot;]=&quot;A&quot;; dt.Rows.Add(dr1); //       dr2[&quot;Id&quot;]=3; dr2[&quot;Name&quot;]=&quot;C&quot;; dr2[&quot;Related&quot;]=&quot;&quot;; dt.Rows.Add(dr2); //       dr3[&quot;Id&quot;]=4; dr3[&quot;Name&quot;]=&quot;D&quot;; dr3[&quot;Related&quot;]=&quot;A&quot;; dt.Rows.Add(dr3); //       dr4[&quot;Id&quot;]=5; dr4[&quot;Name&quot;]=&quot;E&quot;; dr4[&quot;Related&quot;]=&quot;&quot;; dt.Rows.Add(dr4); //         DataView dv = new DataView(ds.Tables[&quot;Customers&quot;]); dv.Sort = &quot;Id ASC&quot;; //       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[&quot;Customers&quot;]); dv1.RowFilter = String.Format(&quot;Related = '{0}'&quot;, dv[0][&quot;Name&quot;]); 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); }    } } </li> On the Build menu, click Build Solution.</li> On the Debug menu, click Start. You notice the behavior that is mentioned in the &quot;Symptoms&quot; section.</li></ol>

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

<div class="references_section">

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

Keywords: kbhotfixserver kbqfe kbbug kbnetframe110sp1fix kbdatabinding kbwindowsforms kbtable kbdll kbcontrol kbqfe kbnetframe110presp1fix kbfix KB828099

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.