设置DataGridView 的当前单元格
/// <summary> /// 设置DGV的当前单元格 /// </summary> /// <param name="dgv">要设置的DGV</param> /// <param name="rowindex">要设置的行</param> /// <param name="columnindex">要设置的列</param> public void SetCurrentCellByDgv(DataGridView dgv,int rowindex, int columnindex) {
//DGV为Null,行或列不存在则不设置
if(dgv==null||0>rowindex ||rowindex>=dgv.RowCount||dgv.Rows[rowindex]==null||0>columnindex ||dgv.Columns.Count <= columnindex)return;
DataGridViewCell cell = this.Rows[rowindex].Cells[columnindex];
if(cell==null)return; dgv.ClearSelection();//取消选择 dgv.CurrentCell = null;//取消当前单元格 dgv.CurrentCell = cell;//设置当前单元格 dgv.Focus();//dgv为窗体焦点 }