Donate

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.
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();                   
         }        
   }  
 }  
This will ensure that databinding has finished.
Cheers!

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

How To Insert Or Add Emojis In Microsoft Teams Status Message

Bootstrap Modal In ASP.NET MVC With CRUD Operations