-2

When ever i try to delete a selected row from the WPF Datagrid it Deletes the row properly but "dataGrid1_SelectionChanged" Event raises an exception that Object Reference is not set to instance of Object. And i am not able to handle this situation after Deletion of row.

private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

        OrignalSelectedRow = null;
        DataGrid gd = (DataGrid)sender;
       DataRowView rowSelected = dataGrid1.SelectedItem as DataRowView;

       int batchnumber = Convert.ToInt32(rowSelected["b_id"]); //This line is raising Exception
       //int batchnumber = batchidval;
       label16.Content = batchnumber;
    }

Row Deletion code is Below

 private void button6_Click(object sender, RoutedEventArgs e)
    {
        //Deletion of Row
        DataRowView rowSelected = dataGrid1.SelectedItem as DataRowView;
        int row_qty = Convert.ToInt32(rowSelected["qty"]);
        int row_batch_num = Convert.ToInt32(rowSelected["b_id"]);
        //label16.Content = row_batch_num;
        label16.Content = row_qty;
        con.Open();
        try
        {
            SqlCommand command = new SqlCommand("update batch set sold_qty=sold_qty-@qty2, left_qty=left_qty+@qty2 where id=@id2", con);
            command.Parameters.AddWithValue("@qty2", row_qty);
            command.Parameters.AddWithValue("@id2", row_batch_num);
            rexe = command.ExecuteNonQuery();
        }
        catch(Exception ex)
        {
            MessageBox.Show("Updation Error "+ex.ToString());
        }
        try
        {
            SqlCommand delcommand = new SqlCommand("DELETE FROM invoice WHERE sr#='" + rowSelected["sr#"] + "'", con);
            //con.Open();
            delcommand.ExecuteNonQuery();
            con.Close();

        }
        catch(Exception ex)
        {
            MessageBox.Show("Deletion Error " + ex.ToString());
        }
        if(rowSelected!=null)
        {
            dt_1.Rows.Remove(rowSelected.Row);   //dt_1 is a DataTable

        }
        dataGrid1.SelectedItem = 0;
    }
yaseen enterprises
  • 141
  • 1
  • 1
  • 8

1 Answers1

1

After removing the row, there will be no selected row. This means

DataRowView rowSelected = dataGrid1.SelectedItem as DataRowView;

is null and you can't access it like

rowSelected["b_id"]

A simple solution is to check null and set a default value if no row is selected:

DataRowView rowSelected = dataGrid1.SelectedItem as DataRowView;
if (rowSelected != null)
{
    int batchnumber = Convert.ToInt32(rowSelected["b_id"]);
    label16.Content = batchnumber;
}
else
{
    label16.Content = "No row selected";
}

A little bit more compact:

if (dataGrid1.SelectedItem is DataRowView rowSelected)
{
    label16.Content = Convert.ToInt32(rowSelected["b_id"]);
}
else
{
    label16.Content = "No row selected";
} 
Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49
  • After removal of row there are also other rows, i want that if row is deleted then if there is any row left then left row is automatically selected. Could this also be acheived. – yaseen enterprises Aug 26 '18 at 05:56
  • Sure, try to set a new selected row after deletion like described at https://stackoverflow.com/questions/6265228/selecting-a-row-in-datagridview-programmatically – Fruchtzwerg Aug 26 '18 at 06:39
  • Please upvote my question as downvotes will cause me to ask further questions. – yaseen enterprises Aug 26 '18 at 11:13