how to insert value into DataGridView Cell?

前端 未结 5 1782
失恋的感觉
失恋的感觉 2021-02-05 14:24

I have DataGridView (that hold any DataBase)

I want to insert any value into any Cell (and that this value will save on DataBase)

How t

5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-05 14:45

    You can use this function if you want to add the data into database, with a button. I hope it will help.

    // dgvBill is name of DataGridView
    
    string StrQuery;
    try
    {
        using (SqlConnection conn = new SqlConnection(ConnectingString))
        {
            using (SqlCommand comm = new SqlCommand())
            {
                comm.Connection = conn;
                conn.Open();
                for (int i = 0; i < dgvBill.Rows.Count; i++) 
                {
                    StrQuery = @"INSERT INTO tblBillDetails (IdBill, productID, quantity, price,  total) VALUES ('" + IdBillVar+ "','" + dgvBill.Rows[i].Cells[0].Value + "', '" + dgvBill.Rows[i].Cells[4].Value + "', '" + dgvBill.Rows[i].Cells[3].Value + "', '" + dgvBill.Rows[i].Cells[2].Value + "');";
                    comm.CommandText = StrQuery;
                    comm.ExecuteNonQuery();         
                 }
             }
         }
     }
     catch (Exception err)
     {
         MessageBox.Show(err.Message  , "Error !");
     }
    

提交回复
热议问题