If you are only working with single selection make sure that you set the mode on the DataGridView:
dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
Now, you can work with the
SelectedCells
property of the
dataGridView
control:
private void btnGetValue_Click(object sender, System.EventArgs e)
{
var selectedcell = dataGridView1.SelectedCells?.Count == 1
? dataGridView1.SelectedCells[0]
: null;
if (selectedcell != null)
{
}
}