locked
DataGridView Deleting Multiple Selected Rows RRS feed

  • Question

  • I am using this code to delete a single selected row from the DataGridView as well as database

    if (dgvTasks.SelectedRows.Count > 0)
    
       {
    
        DataGridViewRow currentRow = dgvTasks.SelectedRows[0];
    
        int Serial = Convert.ToInt32(currentRow.Cells["Serial"].Value);
    
        DialogResult response = MessageBox.Show("Are you sure you want to delete this row?", "Delete row?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
    
        if ((response == DialogResult.Yes))
    
        {
    
         TaskDAL objTaskDal = new TaskDAL();
    
         string message = objTaskDal.DeleteTask(connectionString, Serial);
    
         if (message == "Yes")
    
         {
    
          BindTaskGrid();
    
         }
    
        }
    
       }

     

    Now in order to make multiple selected row I have to run a loop, which is something like this....

     foreach (DataGridViewRow dgRows in dgvTasks.SelectedRows)
     {
    
      dgvTasks.Rows.Remove(dgRows );   
    
     }

    Now inside this loop I have to remove the date elements that are orignally stored in database also along with the row present in DataGridView. So how can i find out if that particular row is selected or not? How can I find the index of that selected row?

    The loop can only runs for the number of row selected. The rows might be randomly selected, its not necessary that they will be in sequence?


    Adeel
    Wednesday, April 28, 2010 7:26 AM

Answers

  • Hi

    Instead you can add a checkbox column,and and you can delete all rows which are checked.

    Also there is a property of Checkbox:True value and False value... set them as 1,0 resp

    and when chkbox val is 1,run the delete query and delete the row from grid as well or you can hide dat particular row.

     

    Amit Govil

    • Marked as answer by maverick786us Wednesday, April 28, 2010 11:41 AM
    Wednesday, April 28, 2010 10:53 AM

All replies

  • Hi

    Instead you can add a checkbox column,and and you can delete all rows which are checked.

    Also there is a property of Checkbox:True value and False value... set them as 1,0 resp

    and when chkbox val is 1,run the delete query and delete the row from grid as well or you can hide dat particular row.

     

    Amit Govil

    • Marked as answer by maverick786us Wednesday, April 28, 2010 11:41 AM
    Wednesday, April 28, 2010 10:53 AM
  • how  ?
    Sunday, April 26, 2015 3:04 AM