说明:View_data 为Datagridview控件, dt为绑定的DataTable数据源
//记录上移
private void btn_up_Click(object sender, EventArgs e)
{
try
{
int i = View_data.CurrentCell.RowIndex; //获取当前所选择的记录行号
object[] _rowData = dt.Rows[i].ItemArray;
dt.Rows[i].ItemArray = dt.Rows[i - 1].ItemArray;
dt.Rows[i - 1].ItemArray = _rowData; //记录上移一行
this.View_data.CurrentCell = this.View_data[this.View_data.CurrentCell.ColumnIndex, this.View_data.CurrentCell.RowIndex - 1]; //选择的光标同时上移一行
}
catch { }
}
//记录下移
private void btn_down_Click(object sender, EventArgs e)
{
try
{
int i = View_data.CurrentCell.RowIndex; //获取当前所选择的记录行号
object[] _rowData = dt.Rows[i].ItemArray;
dt.Rows[i].ItemArray = dt.Rows[i + 1].ItemArray;
dt.Rows[i + 1].ItemArray = _rowData; //记录下移一行
this.View_data.CurrentCell = this.View_data[this.View_data.CurrentCell.ColumnIndex, this.View_data.CurrentCell.RowIndex + 1];//选择的光标同时下移一行
}
catch { }
}
//记录上移
private void btn_up_Click(object sender, EventArgs e)
{
try
{
int i = View_data.CurrentCell.RowIndex; //获取当前所选择的记录行号
object[] _rowData = dt.Rows[i].ItemArray;
dt.Rows[i].ItemArray = dt.Rows[i - 1].ItemArray;
dt.Rows[i - 1].ItemArray = _rowData; //记录上移一行
this.View_data.CurrentCell = this.View_data[this.View_data.CurrentCell.ColumnIndex, this.View_data.CurrentCell.RowIndex - 1]; //选择的光标同时上移一行
}
catch { }
}
//记录下移
private void btn_down_Click(object sender, EventArgs e)
{
try
{
int i = View_data.CurrentCell.RowIndex; //获取当前所选择的记录行号
object[] _rowData = dt.Rows[i].ItemArray;
dt.Rows[i].ItemArray = dt.Rows[i + 1].ItemArray;
dt.Rows[i + 1].ItemArray = _rowData; //记录下移一行
this.View_data.CurrentCell = this.View_data[this.View_data.CurrentCell.ColumnIndex, this.View_data.CurrentCell.RowIndex + 1];//选择的光标同时下移一行
}
catch { }
}