I want get smaller number of Qty from select record in datagridview. after that insert to another Table.

For Example, datagridview A got 5 record with same PONO, when I move these record to datagridview B, they will become a summary record and 5 records become 1 record. like picture below. When record display in datagridview B, the Qyt must be smaller 1.

private void timerFactoryOld_Tick(object sender, EventArgs e)
            SqlCommand cmdCheckID1;
            SqlDataReader dtrCheckID1;

            foreach (DataGridViewRow r in dataGridView1.SelectedRows)

                string PONO = r.Cells[1].Value.ToString();
                string DeliveryDate = r.Cells[2].Value.ToString();
                string Customer = r.Cells[3].Value.ToString();
                string Name = r.Cells[4].Value.ToString();
                string Item = r.Cells[5].Value.ToString();
                string Model = r.Cells[6].Value.ToString();
                string FGrade = r.Cells[7].Value.ToString();
                string Thk = r.Cells[8].Value.ToString();
                string T_Unit = r.Cells[9].Value.ToString();
                string Width = r.Cells[10].Value.ToString();
                string W_Unit = r.Cells[11].Value.ToString();
                string Length = r.Cells[12].Value.ToString();
                string L_Unit = r.Cells[13].Value.ToString();
                string Qty = r.Cells[14].Value.ToString();
                string StockIn = r.Cells[15].Value.ToString();
                string Shortage = r.Cells[16].Value.ToString();
                string ItemPerPackage = r.Cells[17].Value.ToString();
                string Package = r.Cells[18].Value.ToString();
                string Remain = r.Cells[19].Value.ToString();
                bool InternalOrder = Convert.ToBoolean(r.Cells[20].Value.ToString());
                bool Loading = Convert.ToBoolean(r.Cells[21].Value.ToString());
                bool Finished = Convert.ToBoolean(r.Cells[0].Value.ToString());
                bool Urgent = Convert.ToBoolean(r.Cells[22].Value.ToString());
                string ReceivedDate = r.Cells[23].Value.ToString();
                string Remark = r.Cells[24].Value.ToString();
                string Zone = r.Cells[25].Value.ToString();

                //Connectt to Database
                ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["MyDBConnectionString"];
                string name = conSettings.ProviderName;
                string providerName = conSettings.ProviderName;
                string ConnectionString = conSettings.ConnectionString;

                //Code Insert Both values into database table
                SqlConnection con = new SqlConnection(ConnectionString);


                string strChechID1 = "select * from Old where PO_NO='" + PONO + "'";
                cmdCheckID1 = new SqlCommand(strChechID1, con);
                dtrCheckID1 = cmdCheckID1.ExecuteReader();

                SqlCommand comm = new SqlCommand("insert into Old values(@PO_NO,@Model,@Qty,@Remark,@Delivery_Date,@Total_Item)", con);

                Int32 selectedRowCount = dataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected);
                if (selectedRowCount > 0)
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();

                    sb.Append("Total: " + selectedRowCount.ToString());


                string count = selectedRowCount.ToString();


                    if (dtrCheckID1.HasRows)
                        //MessageBox.Show("Duplicate Data!!!!!!");


                        comm.Parameters.AddWithValue("@PO_NO", PONO);
                        comm.Parameters.AddWithValue("@Model", Model);
                        comm.Parameters.AddWithValue("@Qty", Qty);
                        comm.Parameters.AddWithValue("@Remark", Remark);
                        comm.Parameters.AddWithValue("@Delivery_Date", DeliveryDate);
                        comm.Parameters.AddWithValue("@Total_Item", count);


                        lblMessage.Text = ("Successful Insert!!!");



                catch (Exception)

            timerFactoryOld.Enabled = false;
Attachments 1.jpg 30.08 KB 2.jpg 15.93 KB

You are here showing only how you insert data from dgv A into a database.
Do you then populate dgv B from dataBase, of from dgv A?

This article has been dead for over six months. Start a new discussion instead.