說明: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; //記錄上移一(yī)行
this.View_data.CurrentCell = this.View_data[this.View_data.CurrentCell.ColumnIndex, this.View_data.CurrentCell.RowIndex - 1]; //選擇的光标同時上移一(yī)行
}
catch { }
}
//記錄下(xià)移
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; //記錄下(xià)移一(yī)行
this.View_data.CurrentCell = this.View_data[this.View_data.CurrentCell.ColumnIndex, this.View_data.CurrentCell.RowIndex + 1];//選擇的光标同時下(xià)移一(yī)行
}
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; //記錄上移一(yī)行
this.View_data.CurrentCell = this.View_data[this.View_data.CurrentCell.ColumnIndex, this.View_data.CurrentCell.RowIndex - 1]; //選擇的光标同時上移一(yī)行
}
catch { }
}
//記錄下(xià)移
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; //記錄下(xià)移一(yī)行
this.View_data.CurrentCell = this.View_data[this.View_data.CurrentCell.ColumnIndex, this.View_data.CurrentCell.RowIndex + 1];//選擇的光标同時下(xià)移一(yī)行
}
catch { }
}