110,536
社区成员
发帖
与我相关
我的任务
分享
//你的数据源是 绑定的一个DataTable吗?????????
DataTable dt = ds.Table[0]; //数据源
private void button5_Click(object sender, EventArgs e)
{
int index = dataGridView1.CurrentRow.Index; //当前选中行索引号
if (index != 0) //不是第一行时
{
DataRow dr = dt.NewRow(); //新建一行
dt.Rows.InsertAt(dr, index - 1); 插入到上一行
//把原来那行的数据 添加到新建的这行
dt.Rows[index - 1]["列1"] = dataGridView1.CurrentRow.Cells["列1"].Value.ToString();
dt.Rows[index - 1]["列2"] = dataGridView1.CurrentRow.Cells["列2"].Value.ToString();
//移除选中行 由于新添加了一行 所以当前选中行的索引号要+1 即 index+1
dt.Rows.RemoveAt(index + 1);
}
//dataGridView1.DataSource = dt;
}
//这个只是向上的............向下的跟这个应该差不多
//我这里实现整行移动 但是我的选中状态总是停在 index + 1 行 晕死了
//选中行光标整体向下移动
private void button1_Click(object sender, EventArgs e)
{
int index = this.dataGridView1.SelectedRows.Count;
int[] indexs = new int[index];
for (int i = 0; i < index; i++)
{
//获得全部选中行的行下标
indexs[i] = this.dataGridView1.SelectedRows[i].Index;
}
for (int i = 0; i < indexs.Length; i++)
{
if (this.dataGridView1.SelectedRows[0].Index < this.dataGridView1.RowCount-1)
{
this.dataGridView1.Rows[indexs[indexs.Length - 1]].Selected = false;
this.dataGridView1.Rows[indexs[i] + 1].Selected = true;
}
else
{
MessageBox.Show("已经是最后一行了");
break;
}
}
}
DataTable dt = ds.Table[0];
private void button5_Click(object sender, EventArgs e)
{
int index = dataGridView1.CurrentRow.Index;
DataRow dr = dt.NewRow();
dt.Rows.InsertAt(dr, index - 1);
dt.Rows[index - 1]["列1"] = dataGridView1.CurrentRow.Cells["列1"].Value.ToString();
dt.Rows[index - 1]["列2"] = dataGridView1.CurrentRow.Cells["列2"].Value.ToString();
dt.Rows.RemoveAt(index + 1);
}