The current cell refers to the cell in which the DataGridView focus is located, which can be obtained through the CurrentCell property of the DataGridView object. Returns null if the current cell does not exist.
Get the contents of the current cell:
Object obj = This.dgv_PropDemo.CurrentCell.Value;
Note: The return value is of type object.
Gets the column of the current cell index:
int columnindex = This.dgv_PropDemo.CurrentCell.ColumnIndex;
Gets the index of the row where the current cell is located:
int rowindex= This.dgv_PropDemo.CurrentCell.RowIndex;
In addition, use the Datagridview.currentcelladdress property to determine the row where the cell is located:
int row= This.dgv_propdemo.currentcelladdress.y;
Column:
int column = this.dgv_propdemo.currentcelladdress.x;
Note: The row and column indexes for DataGridView are all starting from 0.
The current cell can be changed by setting the CurrentCell of the DataGridView object.
Datagridview1.currentcell=datagridview1[int Columnindex,int RowIndex];
Note: If the selected mode of DataGridView is a row selection, the entire row in which the current cell is located is selected. Otherwise, only the current cell that is set is selected.
Set CurrentCell to NULL to deactivate the current cell.
Example: Setting the first row the second column is the current CurrentCell
This.dgv_PropDemo.CurrentCell = this.dgv_propdemo[1, 0];
Example: Traversing DataGridView with up and down implementations
/// <summary> ///Traverse up/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> Private voidBtn_up_click (Objectsender, EventArgs e) { //gets the index of the previous row intUprowindex = This. Dgv_PropDemo.CurrentCell.RowIndex-1; if(Uprowindex <0) { //Select the last line This. Dgv_propdemo.currentcell = This. dgv_propdemo[0, This. Dgv_propdemo.rowcount-1]; } Else { This. Dgv_propdemo.currentcell = This. dgv_propdemo[0, Uprowindex]; } } /// <summary> ///Traverse down/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> Private voidBtn_down_click (Objectsender, EventArgs e) { //gets the index of the next row intNextrowindex = This. Dgv_PropDemo.CurrentCell.RowIndex +1; if(Nextrowindex > This. Dgv_propdemo.rowcount-1) { This. Dgv_propdemo.currentcell = This. dgv_propdemo[0,0]; } Else { This. Dgv_propdemo.currentcell = This. dgv_propdemo[0, Nextrowindex]; } }
DataGridView Gets or sets the contents of the current cell