datagridview 自动排序后,在用下面的方法无法移动行,估计是被排序功能锁定了,大虾们谁知道如何解决呀!
上移下移行代码如下://上移
private void btnUp_Click(object sender, EventArgs e)
{
DataTable dt = (DataTable)dataGridView1.DataSource;
int index = dataGridView1.SelectedRows[0].Index;if (dataGridView1.CurrentRow.Index <= 0)
{
return;
}
else
{
DataRow tempRow = dt.NewRow();
object[] dRowObj = dt.Rows[index].ItemArray;
tempRow.ItemArray = dRowObj ;
dt.Rows.InsertAt(tempRow, index - 1);
dt.Rows.RemoveAt(index + 1);
dataGridView1.ClearSelection();
dataGridView1.Rows[index - 1].Selected = true;
dataGridView1.DataSource = dt;
}
}
这是不是MS的bug呀
上移下移行代码如下://上移
private void btnUp_Click(object sender, EventArgs e)
{
DataTable dt = (DataTable)dataGridView1.DataSource;
int index = dataGridView1.SelectedRows[0].Index;if (dataGridView1.CurrentRow.Index <= 0)
{
return;
}
else
{
DataRow tempRow = dt.NewRow();
object[] dRowObj = dt.Rows[index].ItemArray;
tempRow.ItemArray = dRowObj ;
dt.Rows.InsertAt(tempRow, index - 1);
dt.Rows.RemoveAt(index + 1);
dataGridView1.ClearSelection();
dataGridView1.Rows[index - 1].Selected = true;
dataGridView1.DataSource = dt;
}
}
这是不是MS的bug呀
你只能在数据绑定之前在DataTable中进行排序了,不要在DataGridView中进行排序
因为你在DataGridView中进行排序了,在更改它的顺序,这必然照成混乱。 所以还是在绑定DataGridView的数据源之前进行排序!
代码如下
CurrencyManager manager = this.BindingContext[dtblSource] as CurrencyManager;
DataView dtView =manager.List as DataView;
dtView.Sort = "";
将排序清调就行了