我在一datagrid中有4笔数据
1  xxx
2  yyy
3  zzz
4  avsd
其中1, 2为新增行, 3, 4为已存在的行
我点击datagrid的header排序后变为
4  avsd
3  zzz
2  yyy
1  xxx
可是为什么行状态会变为4,3为新增行; 2,1变为原来4,3行的状态啊
也就是说排序后只把数据换了行,可是行的状态没有跟着换,要怎么解决啊

解决方案 »

  1.   

    看看datagrid key的设置是不是有问题
      

  2.   

    什么datagrid key?
    我的是winform,根本就没什么datagrid key
      

  3.   

    你可能是用DataTable.Rows[int]获取DataRow的
    用DataTable.DefaulView[DataGrid.CurrentRowIndex].Row来获取DataRow试试吧
      

  4.   

    DataView改变的顺序,其实不会改变DataTable里的DataRow顺序。
    也就是说,虽然你现在看到的是
    4
    3
    2
    1
    而DataTable里还是
    1
    2
    3
    4
      

  5.   

    可以使用DataTable.Select来排序。
    private static void GetRowsByFilter()
    {
        
        DataTable customerTable = new DataTable( "Customers" );
        // Add columns
        customerTable.Columns.Add( "id", typeof(int) );
        customerTable.Columns.Add( "name", typeof(string) );    // Set PrimaryKey
        customerTable.Columns[ "id" ].Unique = true;
        customerTable.PrimaryKey = new DataColumn[] { customerTable.Columns["id"] };    // Add ten rows
        for( int id=1; id<=10; id++ )
        {
            customerTable.Rows.Add( 
                new object[] { id, string.Format("customer{0}", id) } );
        }
        customerTable.AcceptChanges();    // Add another ten rows
        for( int id=11; id<=20; id++ )
        {
            customerTable.Rows.Add( 
                new object[] { id, string.Format("customer{0}", id) } );
        }    string strExpr;
        string strSort;
        
        strExpr = "id > 5";
        // Sort descending by column named CompanyName.
        strSort = "name DESC";
        // Use the Select method to find all rows matching the filter.
        DataRow[] foundRows = 
            customerTable.Select( strExpr, strSort, DataViewRowState.Added );
        
        PrintRows( foundRows, "filtered rows" );    foundRows = customerTable.Select();
        PrintRows( foundRows, "all rows" );
    }
      

  6.   

    你的DataGrid是否绑定到DataView?