0

i was searching before about this matter but some answer i get seems still cannot fix my problem here my code

 private void delete_record()
        {
            if (menuGrid.Rows.Count > 1 && menuGrid.SelectedRows[0].Index != menuGrid.Rows.Count - 1)
            {
                query = "DELETE FROM produk WHERE sn=" + menuGrid.SelectedRows[0].Cells[0].Value.ToString() + "";
                cmd = new MySqlCommand(query,connection);
                menuGrid.Rows.RemoveAt(menuGrid.SelectedRows[0].Index);
                MessageBox.Show("deleted");
            }
        } 

i tried change inside if() whith simples index and it still have same error, event this code

 private void delete_record()
        {
                query = "DELETE FROM produk WHERE sn=" + menuGrid.SelectedRows[0].Cells[0].Value.ToString() + "";
                cmd = new MySqlCommand(query,connection);
                menuGrid.Rows.RemoveAt(menuGrid.SelectedRows[0].Index);
                MessageBox.Show("deleted");
         } 

still give me same error. hope some one can tell me where i was wrong and can fix it

GT000
  • 1
  • 1
  • Either `SelectedRows` contains no items, or `Cells` for the selected row contains no items. Note that `menuGrid.Rows.Count` isn't checking if any rows are selected, it's merely checking that rows exist (and that more than one row exists at that). – ProgrammingLlama Mar 03 '21 at 10:05
  • 1
    Never us `SelectedRows` without checking `SelectedRows.Any()` first! – TaW Mar 03 '21 at 10:07
  • @John so i cant merely pick row and cells direcly from database? – GT000 Mar 03 '21 at 10:13
  • @TaW i have to make declaration SelectedRows.Any() first before use SelectedRows? – GT000 Mar 03 '21 at 10:14
  • 1
    I'm not sure, but I sense there might be some misunderstanding here. "Selected" in this context doesn't mean "rows I've SELECTed from the database". It means "rows in the datagrid that the user has selected on the UI". – ProgrammingLlama Mar 03 '21 at 10:17
  • Yes, you should do this test instead of relying blindly on some row being selected. – TaW Mar 03 '21 at 10:17
  • @Kohn, yup, good catch, this may indeed be the reason for the code.. – TaW Mar 03 '21 at 10:18
  • sorry late reply. thx for answering my question. for past this week i tried learn more about row and cell and your answer been helpfull for me to know more – GT000 Mar 09 '21 at 04:54

0 Answers0