Click here to Skip to main content
16,017,899 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello.....
I have a Datagridview . and there two columns exist ,one is DataGridViewTextBoxColumn(zeroth column)
and another is DataGridViewCheckBoxColumn(first column)
I have already filled my Datagridview. i want to delete those rows which are Checked.
my code is ...

C#
for (int i = 1; i <= dataGridView1.Rows.Count; i++)
      {
        if  (Convert.ToBoolean(dataGridView1.Rows[i].Cells[1].Value)==true)
           {
              dataGridView1.Rows.RemoveAt ( i);
           }   
      }


but my selected rows could not delete , exception is occurred ,
it shows that

C#
dataGridView1.Rows[1].Cells[1].Value =null  //if I check 1st Row


why is shows null though it is already checked ?

any other code to delete checked rows ?
Posted
Updated 13-Sep-12 1:23am
v2

Try this:
C#
protected void btnMultipleRowDelete_Click(object sender, EventArgs e)
{
    // Looping through all the rows in the GridView
    foreach (GridViewRow row in GridView1.Rows)
    {
        CheckBox checkbox = (CheckBox)row.FindControl("cbRows");

        //Check if the checkbox is checked.
        //value in the HtmlInputCheckBox's Value property is set as the

        //value of the delete command's parameter.
        if (checkbox.Checked)
        {
            // Retreive the Employee ID
            int employeeID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);

            // Pass the value of the selected Employye ID to the Delete //command.
            SqlDataSource1.DeleteParameters["EmployeeID"].DefaultValue = employeeID.ToString();
            SqlDataSource1.Delete();
        }
    }
}

Get complete answer on following thread:
Delete rows from datgridview and database access[^]
 
Share this answer
 
Comments
krushna chandra jena 13-Sep-12 7:34am    
My Project is Windows Desktop Application based.....Please provide another code.
Prasad_Kulkarni 13-Sep-12 7:38am    
My bad. Have a look on a similar discussion[^]. Hope you get some help out of it.
Hope it helps:
C#
List<int> indexes =  new List<int>();

foreach (DataGridViewRow row in dataGridView1.Rows)
{
   if (row.Cells[1].Value != null)
      if ((bool)row.Cells[1].Value)
         indexes.Add(row.Index);
}

foreach (int index in indexes)
    dataGridView1.RemoveAt(index);</int></int>
 
Share this answer
 
C#
for (int i = 1; i <= dataGridView1.Rows.Count; i++)

you are starting loop from 1. That is why you are getting error.
start for loop from 0th index
C#
for (int i = 0; i < dataGridView1.Rows.Count; i++)
 
Share this answer
 
Comments
krushna chandra jena 14-Sep-12 1:40am    
Yeah yes work....
but can you explain why it does not work if it starts from first(1st) position
pradiprenushe 14-Sep-12 1:54am    
Suppose you are having 5 record in datagridview.
dataGridView1.Rows.Count will be 5.
datagridview rows index starts from 0 & ends at 4.(0,1,2,3,4)
So you can handle row by 0,1,2,3,4.(if you start from 0)
1,2,3,4(if you start from 1)
when you use 0 means first row, 1 means second row.
Your loop goes to 5 means sixth row which is not present in datagridview. So you are getting exception.
Hi,

try like this,

C#
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
//Get the appropriate cell using index, name or whatever and cast to DataGridViewCheckBoxCell
DataGridViewCheckBoxCell cell = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells[1];

//Compare to the true value because Value isn't boolean
if (cell.Value == cell.TrueValue)
{   
//The value is true
  
  dataGridView1.Rows.RemoveAt(i);
  i = ((i - 1) < 0) ? 0 : (i - 1); 
 //Since the Rows count decremented by 1. 
 //So, decrement the loop counter by 1        
}   
}


hope it works.
 
Share this answer
 
v3
Comments
krushna chandra jena 13-Sep-12 7:43am    
But my question is if it does not check the condition then how you set the value of i.
at first you have to fulfill the condition then you can go inside of the loop other wise it still shows
Convert.ToBoolean(dataGridView1.Rows[i].Cells[1].Value) = null
Karthik Harve 13-Sep-12 7:50am    
Try with my updated answer.
Hi Use this code

C#
if (((CheckBox)(dataGridView1.Rows[i].Cells[1].FindControl("checkBoxName"))).Checked  == true)
           {
               dataGridView1.Rows.RemoveAt(i);
           }


Hope this will work
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900