Skip to content

How Insert data in table using data datagridview

Karan Gupta

Asked Apr 7, 2015, 12:52 PM EDT1 answer
Share
LinkedInX
hiii friends
 
project spacification
- windows form application in c#
- sql database (service based)
 example-
i have datagridview  with checkbox  and one DELETE button in my project . and i have 2 table in my database.  table1 and table2 both of table have same number of column and same datatype.
 
in datagridview i'm displaying the  data of table1.   i want to perform a DELETE operation and after deleting that data from table1 it  should be moved in table2 abd have have to be delete from its origion table1.
 
i have created it but what happening after if i'm selecting 3 rows ... 1st row moving in table1 to table2 but after that another
2rows are not moved in table2.  
 
i'have attached my code below.

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    private void Deleteb(object sender, EventArgs e)
            {
                

             
                int h;
                    List<DataGridViewRow> selectedRows = (from row in dataGridView1.Rows.Cast<DataGridViewRow>()
                                                          where Convert.ToBoolean(row.Cells["column2"].Value) == true
                                                          select row).ToList();
                    if (MessageBox.Show(string.Format("Do you want to Place  {0} items order ?", selectedRows.Count), "Confirmation", MessageBoxButtons.YesNo) == DialogResult.Yes)
             


                    foreach (DataGridViewRow row in selectedRows)
                    {




                        try
                        {
                           


                            //string StrQuery = "INSERT INTO placedorder  VALUES ( '" + dataGridView1.Rows[i].Cells["pcode"].Value + "', '" + dataGridView1.Rows[i].Cells["name"].Value + "', '" + dataGridView1.Rows[i].Cells["weight"].Value + "', '" + dataGridView1.Rows[i].Cells["quantity"].Value + "', '" + dataGridView1.Rows[i].Cells["date"].Value + "', '" + dataGridView1.Rows[i].Cells["supplier"].Value + "')";
                            string StrQuery = "INSERT INTO table2  VALUES ( '" + row.Cells["pcode"].Value + "', '" + row.Cells["name"].Value + "', '" + row.Cells["weight/gm"].Value + "', '" + row.Cells["quantity"].Value + "', '" + row.Cells["mfg"].Value + "', '" + row.Cells["date"].Value + "', '" + comboBox1.Text + "')";


                            SqlCommand com = new SqlCommand(StrQuery, con);
                            con.Open();
                            h = com.ExecuteNonQuery();
                            con.Close();


                            if (h > 0)
                            {
                           
                                MessageBox.Show("imsertredddd", "Confirmation");
                               
                                


                                
                                List<DataGridViewRow> selectedRows1 = (from row1 in dataGridView1.Rows.Cast<DataGridViewRow>()
                                                                       where Convert.ToBoolean(row1.Cells["column2"].Value) == true
                                                                       select row1).ToList();
                                // if (MessageBox.Show(string.Format("Order sucessfully Done. Now you want to give this order to  another one also  ?", selectedRows1.Count), "Confirmation", MessageBoxButtons.YesNo) == DialogResult.No)


                           
                                foreach (DataGridViewRow row1 in selectedRows1)
                                {


                                    con.Open();
                                       com = new SqlCommand("delete from table1 where Pcode='" + row1.Cells["pcode"].Value + "'", con);
                                    int h1 = com.ExecuteNonQuery();
                                    con.Close();
                                    if (h1 > 0)
                                    {
                                        MessageBox.Show("DeletedOrder sucessfully done to " + comboBox1.Text);


                                       
                                    }
                                    else
                                    {
                                       
                                    }


                                }
                            }
                            else
                            {


                                
                            }
















                        }










                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                        finally
                        {
                           
                           
                           
                           
                         
                        }
                    }


                   
               
            }
    0