WinForm方面的 急!!!!! DataGridView绑定XML数据,并实现行的上下移动。现在用XML绑定已经做到了。但如何实现在DataGridView中让选中的行上移或下移? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 只是在DataGridView中移动,要不要反映在数据库里呢? 数据是用XML绑定的。也需要改XML里的顺序 //向上移动 private void button4_Click(object sender, EventArgs e) { if (dataGridView1.SelectedRows.Count == 0) { return; } DataTable dt =(DataTable) this.dataGridView1.DataSource; int i = this.dataGridView1.Rows.IndexOf(dataGridView1.SelectedRows[0]); object [] ot = dt.Rows[i].ItemArray; DataRow dr = dt.Rows[i]; dt.Rows.Remove(dr); DataRow drs = dt.NewRow(); drs.ItemArray = ot; dt.Rows.InsertAt(drs, i - 1); foreach (DataGridViewRow dgvr in dataGridView1.SelectedRows) { dgvr.Selected = false; } dataGridView1.Rows[i - 1].Selected = true; dataGridView1.CurrentCell = dataGridView1.Rows[i - 1].Cells[0]; }在网上学的,但是提示错误。无法将类型为“System.Data.DataSet”的对象强制转换为类型“System.Data.DataTable”。 //Call SwitchDataGridViewRow Method private void button1_Click(object sender, EventArgs e) { SwitchDataGridViewRow(0, 1); } //Switch DataGridViewRow private void SwitchDataGridViewRow(int nRow1, int nRow2) { DataGridViewRow tmp = new DataGridViewRow(); tmp.CreateCells(dgv_main); for (int i = 0; i < dgv_main.Columns.Count; i++) { tmp.Cells[i].Value = dgv_main.Rows[nRow1].Cells[i].Value; dgv_main.Rows[nRow1].Cells[i].Value = dgv_main.Rows[nRow2].Cells[i].Value; dgv_main.Rows[nRow2].Cells[i].Value = tmp.Cells[i].Value; } } 那你面对的问题其实只是把datagridview的数据写到xml里而已 这短代码能实现对DataGridView 上下移动吗?麻烦下能不能把上调和下调的代码分别写下啊?上调用button4 下调用button5表示 以前写的:http://blog.csdn.net/wxm3630478/article/details/4256851 不就是相邻行调个位置么?但愿我的理解是对的。假使你选中第n行,那么:SwitchDataGridViewRow(n-1, n);//上移SwitchDataGridViewRow(n, n+1);//下移 treeview,datagridview控件的联合使用! 关于回文数 这个正则表达式怎么写? 请问FastReport怎么样才能连到ADO.NET的数据! 泛型的问题 求助!!!! 各位大虾请帮帮小妹,感激不尽 请问如何能用ASP.NET实现像CSDN这样将帖存在XML里,能不能给些思路和相关文章呀??谢啦! 求思路,源码,资料。关于邮件群发 水晶报表导出出错?为何! WPF grid 重叠问题 Tracking Analyst方面:ITimeControl使用问题 C#
数据是用XML绑定的。也需要改XML里的顺序
//向上移动
private void button4_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count == 0)
{
return;
}
DataTable dt =(DataTable) this.dataGridView1.DataSource;
int i = this.dataGridView1.Rows.IndexOf(dataGridView1.SelectedRows[0]);
object [] ot = dt.Rows[i].ItemArray;
DataRow dr = dt.Rows[i];
dt.Rows.Remove(dr);
DataRow drs = dt.NewRow();
drs.ItemArray = ot;
dt.Rows.InsertAt(drs, i - 1);
foreach (DataGridViewRow dgvr in dataGridView1.SelectedRows)
{
dgvr.Selected = false;
}
dataGridView1.Rows[i - 1].Selected = true;
dataGridView1.CurrentCell = dataGridView1.Rows[i - 1].Cells[0];
}在网上学的,但是提示错误。
无法将类型为“System.Data.DataSet”的对象强制转换为类型“System.Data.DataTable”。
private void button1_Click(object sender, EventArgs e)
{
SwitchDataGridViewRow(0, 1);
} //Switch DataGridViewRow
private void SwitchDataGridViewRow(int nRow1, int nRow2)
{
DataGridViewRow tmp = new DataGridViewRow();
tmp.CreateCells(dgv_main);
for (int i = 0; i < dgv_main.Columns.Count; i++)
{
tmp.Cells[i].Value = dgv_main.Rows[nRow1].Cells[i].Value;
dgv_main.Rows[nRow1].Cells[i].Value = dgv_main.Rows[nRow2].Cells[i].Value;
dgv_main.Rows[nRow2].Cells[i].Value = tmp.Cells[i].Value;
}
}
这短代码能实现对DataGridView 上下移动吗?麻烦下能不能把上调和下调的代码分别写下啊?上调用button4 下调用button5表示
http://blog.csdn.net/wxm3630478/article/details/4256851
SwitchDataGridViewRow(n, n+1);//下移