Training
Learning path
Work with variable data in C# console applications (Get started with C#, Part 4) - Training
Work with variable data in C# console applications (Get started with C#, Part 4)
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Interaction with the DataGridView often requires that you programmatically discover which cell is currently active. You may also need to change the current cell. You can perform these tasks with the CurrentCell property.
Note
You cannot set the current cell in a row or column that has its Visible property set to false
.
Depending on the DataGridView control's selection mode, changing the current cell can change the selection. For more information, see Selection Modes in the Windows Forms DataGridView Control.
Use the DataGridView control's CurrentCell property.
private void getCurrentCellButton_Click(object sender, System.EventArgs e)
{
string msg = String.Format("Row: {0}, Column: {1}",
dataGridView1.CurrentCell.RowIndex,
dataGridView1.CurrentCell.ColumnIndex);
MessageBox.Show(msg, "Current Cell");
}
Private Sub getCurrentCellButton_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles getCurrentCellButton.Click
Dim msg As String = String.Format("Row: {0}, Column: {1}", _
dataGridView1.CurrentCell.RowIndex, _
dataGridView1.CurrentCell.ColumnIndex)
MessageBox.Show(msg, "Current Cell")
End Sub
Set the CurrentCell property of the DataGridView control. In the following code example, the current cell is set to row 0, column 1.
private void setCurrentCellButton_Click(object sender, System.EventArgs e)
{
// Set the current cell to the cell in column 1, Row 0.
this.dataGridView1.CurrentCell = this.dataGridView1[1,0];
}
Private Sub setCurrentCellButton_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles setCurrentCellButton.Click
' Set the current cell to the cell in column 1, Row 0.
Me.dataGridView1.CurrentCell = Me.dataGridView1(1, 0)
End Sub
This example requires:
Button controls named getCurrentCellButton
and setCurrentCellButton
. In Visual C#, you must attach the Click events for each button to the associated event handler in the example code.
A DataGridView control named dataGridView1
.
References to the System and System.Windows.Forms assemblies.
.NET Desktop feedback feedback
.NET Desktop feedback is an open source project. Select a link to provide feedback:
Training
Learning path
Work with variable data in C# console applications (Get started with C#, Part 4) - Training
Work with variable data in C# console applications (Get started with C#, Part 4)