//下移一行
private void button2_Click( object sender, EventArgs e )
{
DataGridViewRow dr = dataGridView1.CurrentRow;
if ( dr.Index < dataGridView1.Rows.Count - 1 )
{
int index = dr.Index;
dataGridView1.Rows.Remove( dr );
dataGridView1.Rows.Insert( index + 1, dr );
dr.Selected = true;
dataGridView1.CurrentCell = dr.Cells[0];
}
}
//上移一行
private void button3_Click( object sender, EventArgs e )
{
DataGridViewRow dr = dataGridView1.CurrentRow;
if ( dr.Index > 0 )
{
int index = dr.Index;
dataGridView1.Rows.Remove( dr );
dataGridView1.Rows.Insert( index - 1, dr );
dr.Selected = true;
dataGridView1.CurrentCell = dr.Cells[0];
}
}
//首行
private void button4_Click( object sender, EventArgs e )
{
if ( dataGridView1.CurrentRow.Index != 0 )
{
DataGridViewRow dr = dataGridView1.CurrentRow;
dataGridView1.Rows.Remove( dr );
dataGridView1.Rows.Insert(0, dr );
dr.Selected = true;
dataGridView1.CurrentCell = dr.Cells[0];
}
}
//末行
private void button5_Click( object sender, EventArgs e )
{
if ( dataGridView1.CurrentRow.Index <dataGridView1.Rows.Count-1 )
{
DataGridViewRow dr = dataGridView1.CurrentRow;
dataGridView1.Rows.Remove( dr );
dataGridView1.Rows.Insert( dataGridView1.Rows.Count , dr );
dr.Selected = true;
dataGridView1.CurrentCell = dr.Cells[0];
}
}
本文介绍了一个使用C#编写的DataGridView控件中的行操作方法,包括行的上移、下移、移动到首行及末行的功能实现。通过这些方法,用户可以方便地对表格中的数据进行重新排序。

2456

被折叠的 条评论
为什么被折叠?



