问题描述
您好,我有一个与bindingSource关联的DataGridView:
Hello, I have a DataGridView associated to a bindingSource:
bindingSource1.DataSource = dataset1;
bindingSource1.DataMember = "table1";
dataNavigator1.DataSource = bindingSource1;
dataGridView1.DataSource = bindingSource1;
我有两个功能可以上下移动选定的行:
And I have two functions to move up and down a selected row:
private void buttonUp_Click(object sender, EventArgs e)
{
int index = this.bindingSource1.Position;
if (index > 0)
{
DataRow dr = (DataRow)this.dataset1["table1"].Rows[index];
DataRow newDr = this.dataset1["table1"].NewRow();
newDr.ItemArray = dr.ItemArray;
this.dataset1["table1"].Rows.RemoveAt(index);
this.dataset1["table1"].Rows.InsertAt(newDr, index - 1);
this.bindingSource1.Position = index - 1;
}
}
private void buttonDown_Click(object sender, EventArgs e)
{
int index = this.bindingSource1.Position;
if (index < this.bindingSource1.Count)
{
DataRow dr = (DataRow)this.dataset1["table1"].Rows[index];
DataRow newDr = this.dataset1["table1"].NewRow();
newDr.ItemArray = dr.ItemArray;
this.dataset1["table1"].Rows.RemoveAt(index);
this.dataset1["table1"].Rows.InsertAt(newDr, index + 1);
this.bindingSource1.Position = index + 1;
}
}
两种方法都可以正常工作,当我单击按钮以移动行时,它会正确移动.
但是,如果我在某列上单击以对其进行排序(在Header上单击),然后在尝试再次移动一行后,它的绑定源位置被移动了,但是datagridview中的行却没有移动.
我调试了功能,没有任何问题,这似乎只是datagridview可视化错误.
我试图在dataGridView1_Sorted处理的事件中扩展绑定源上的排序,但是仍然无法正常工作.
为什么对datagridview进行排序操作后行不移动?
谢谢.
-Alessandro
The two methods works fine and when I click on button to move the row, it gets correctly moved.
But if I click before on a column in order to sort it (click on Header) and after try to move again a row, the binding source position it''s moved but the row in the datagridview not.
I debug the functions and nothing goes wrong, it seems just a datagridview visualization error.
I tried to expand the sorting on the binding source, in the dataGridView1_Sorted handled event but still doesn''t works.
Why the row is not moved after a sort operation on a datagridview?
thanks.
-Alessandro
推荐答案
public partial class Form1 : Form
{
DataTable dt;
DataSet ds = new DataSet();
public Form1()
{
InitializeComponent();
dt = new DataTable("table1");
dt.Columns.Add("Column1", typeof(int));
dt.Columns.Add("Column2", typeof(int));
dt.Columns.Add("Column3", typeof(int));
dt.Rows.Add(1, 0, 0);
dt.Rows.Add(2, 1, 1);
dt.Rows.Add(2, 0, 0);
dt.Rows.Add(3, 1, 1);
dt.Rows.Add(3, 0, 4);
dt.Rows.Add(3, 3, 4);
ds.Tables.Add(dt);
bindingSource1.DataSource = ds.Tables[0];
this.dataGridView1.DataSource = bindingSource1;
}
private void dataGridView1_Sorted(object sender, EventArgs e)
{
//force the BindingSource to reflect the same sort order as the DataGridView
String sort = dataGridView1.SortedColumn.DataPropertyName;
if (dataGridView1.SortOrder == SortOrder.Descending)
{
sort = sort + " DESC";
}
//ds.Tables["table1"].DefaultView.Sort = sort;
bindingSource1.Sort = sort;
}
private void button1_Click(object sender, EventArgs e)
{
bindingSource1.Sort = "";
//ds.Tables[0].DefaultView.Sort = "";
int index = this.bindingSource1.Position;
if (index > 0)
{
DataRow dr = (DataRow)this.ds.Tables["table1"].Rows[index];
DataRow newDr = this.ds.Tables["table1"].NewRow();
newDr.ItemArray = dr.ItemArray;
this.ds.Tables["table1"].Rows.RemoveAt(index);
this.ds.Tables["table1"].Rows.InsertAt(newDr, index - 1);
this.bindingSource1.Position = index - 1;
}
}
private void button2_Click(object sender, EventArgs e)
{
bindingSource1.Sort = "";
int index = this.bindingSource1.Position;
if (index < this.bindingSource1.Count)
{
DataRow dr = (DataRow)this.ds.Tables["table1"].Rows[index];
DataRow newDr = this.ds.Tables["table1"].NewRow();
newDr.ItemArray = dr.ItemArray;
this.ds.Tables["table1"].Rows.RemoveAt(index);
this.ds.Tables["table1"].Rows.InsertAt(newDr, index + 1);
this.bindingSource1.Position = index + 1;
}
}
}
这篇关于datagridview列排序后,MoveUp/MoveDown行不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!