DataGridview Current Row Returns Null In SelectionChanged Event
When retrieving selected row cell values in the DataGridview, you might encounter object not set to reference of an instance. It's because the current row has not been initialized. Check first the cell value of the current row if it has cell contents.
This will ensure that databinding has finished.
Cheers!
private void dgvAccounts_SelectionChanged(object sender, EventArgs e) { string countryName = string.Empty; if (dgvAccounts.SelectedRows.Count > 0) { if (dgvAccounts.CurrentRow.Cells[1].Value != null) { countryName = dgvAccounts.CurrentRow.Cells[1].Value.ToString(); } } }
Cheers!
Comments
Post a Comment