Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
This code not delete from datagrid Randomly
I need to select any row to delete but this code delete just from first row then second row etc.
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                DataGridViewRow dr = dataGridView1.Rows[i];
                if (dr.Selected == true)
                {
                    try
                    {
                        SqlCommand cmd = new SqlCommand("Delete from calender where ID='" + dataGridView1.SelectedRows[i].Cells[2].Value + "'", con);
                        cmd.ExecuteNonQuery();
                        con.Close();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                    dataGridView1.Rows.RemoveAt(i);
                }
            }
Posted 20-Nov-12 10:20am
Edited 20-Nov-12 10:32am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

if (this.dataGridView1.SelectedRows.Count > 0)
           {
               bs.RemoveAt(this.dataGridView1.SelectedRows[0].Index);
           }
           else
           {
               MessageBox.Show("Please select one row");
           }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Try this
for (int i = 0; i < dataGridView1.SelectedRows.Count; i++)
            {              
               
                    try
                    {
                        SqlCommand cmd = new SqlCommand("Delete from calender where ID='" + dataGridView1.SelectedRows[i].Cells[2].Value + "'", con);
                        cmd.ExecuteNonQuery();
                        con.Close();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                    dataGridView1.Rows.RemoveAt(i);
                
            }
 
 
foreach(DataGridViewRow dr in dataGridView1.SelectedRows)
{
 try
                    {
                        SqlCommand cmd = new SqlCommand("Delete from calender where ID='" + dr.Cells[2].Value + "'", con);
                        cmd.ExecuteNonQuery();
                        con.Close();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                    dataGridView1.Rows.RemoveAt(i);
}
  Permalink  
v2
Comments
meme_307 at 21-Nov-12 7:30am
   
Tank's it works good :)

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



Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 21 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100