我在一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 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行的状态啊
也就是说排序后只把数据换了行,可是行的状态没有跟着换,要怎么解决啊
我的是winform,根本就没什么datagrid key
用DataTable.DefaulView[DataGrid.CurrentRowIndex].Row来获取DataRow试试吧
也就是说,虽然你现在看到的是
4
3
2
1
而DataTable里还是
1
2
3
4
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" );
}