上移
private void buttonX7_Click(object sender, EventArgs e)
{
if (this.dataGridViewX1.SelectedRows[0] == null)
{
return;
}
int rowIndex = this.dataGridViewX1.SelectedRows[0].Index; //得到当前选中行的索引
if (rowIndex == 0)
{
MessageBox.Show("已经是第一行了!");
return;
}
List<string> list = new List<string>();
for (int i = 1; i < this.dataGridViewX1.Columns.Count; i++)
{
list.Add(IsNull(this.dataGridViewX1.SelectedRows[0].Cells[i].Value)); //把当前选中行的数据存入list数组中
}
for (int j = 1; j < this.dataGridViewX1.Columns.Count; j++)
{
this.dataGridViewX1.Rows[rowIndex].Cells[j].Value = this.dataGridViewX1.Rows[rowIndex - 1].Cells[j].Value;
this.dataGridViewX1.Rows[rowIndex - 1].Cells[j].Value = list[j - 1].ToString();
}
this.dataGridViewX1.Rows[rowIndex - 1].Selected = true;
this.dataGridViewX1.Rows[rowIndex].Selected = false;
}
下移
private void buttonX8_Click(object sender, EventArgs e)
{
if (this.dataGridViewX1.SelectedRows[0] == null)
{
return;
}
int rowIndex = this.dataGridViewX1.SelectedRows[0].Index; //得到当前选中行的索引
if (rowIndex == this.dataGridViewX1.Rows.Count - 1)
{
MessageBox.Show("已经是最后一行了!");
return;
}
List<string> list = new List<string>();
for (int i = 1; i < this.dataGridViewX1.Columns.Count; i++)
{
list.Add(IsNull(this.dataGridViewX1.SelectedRows[0].Cells[i].Value)); //把当前选中行的数据存入list数组中
}
for (int j = 1; j < this.dataGridViewX1.Columns.Count; j++)
{
this.dataGridViewX1.Rows[rowIndex].Cells[j].Value = this.dataGridViewX1.Rows[rowIndex + 1].Cells[j].Value;
this.dataGridViewX1.Rows[rowIndex + 1].Cells[j].Value = list[j - 1].ToString();
}
this.dataGridViewX1.Rows[rowIndex + 1].Selected = true;
this.dataGridViewX1.Rows[rowIndex].Selected = false;
}