junaid khurshid

junaid khurshid

  • NA
  • 52
  • 6.7k

datagrid in Wpf

Jun 29 2014 1:06 AM
  I am using datagrid in my usercontrol in wpf
 
I   am doing edit delete using selection change event . I edit or delete data from datagrid when i call datagrid load function it gives me exception "index out of range  must  be non negative......" my grid load function is this
private void grid_load()
{
con.Open();
   SqlCommand cmd = new SqlCommand("Select commanNameId, commanName As CommanName ,date As Date from commanNametbl where                     catageoryId='" + idlbl.Text + "' Order By commanName ASC", con);
   cmd.ExecuteNonQuery();
      con.Close();
      SqlDataAdapter dap = new SqlDataAdapter(cmd);
      DataTable dt = new DataTable();
      dap.Fill(dt);
      dataGrid1.ItemsSource = dt.DefaultView;
      dataGrid1.UpdateLayout();
      dataGrid1.Columns[0].Visibility = Visibility.Hidden;
}
 and my delete button code is this
private void delbtn_Click(object sender, RoutedEventArgs e)
     con.Open();
      SqlCommand cmd = new SqlCommand("Delete from commanNametbl where commanNameId='" + comBlock.Text + "'", con);
      int ii = cmd.ExecuteNonQuery();
      con.Close();
      comanNametextBox.Text = "";
      comNametextBlock.Text = "";
      MessageBox.Show("Record Deleted Sucessfully!");
      grid_load(); 
}
Thanks in Advance

Answers (3)