Cara Simpan data Gridview ke database
Ini Kode Nya :
C# :
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[0].Value == null && row.Cells[1].Value == null && row.Cells[2].Value == null && row.Cells[3].Value == null && row.Cells[4].Value == null && row.Cells[5].Value == null && row.Cells[6].Value == null && row.Cells[7].Value == null)
{
}
else
{
con.Open();
OleDbCommand cmd = new OleDbCommand("insert into Sold(Product_name,Model_no,Seater,Leather,Colour,Unit_price,Quantity,Total,Advancepayment,Duespayment) values (@Product_name,@Model_no,@Seater,@Leather,@Colour,@Unit_price,@Quantity,@Total,@Advancepayment,@Duespayment)", con);
cmd.Parameters.AddWithValue("@Product_name", row.Cells[0].Value.ToString());
cmd.Parameters.AddWithValue("@Model_no", row.Cells[1].Value.ToString());
cmd.Parameters.AddWithValue("@Seater", row.Cells[2].Value.ToString());
cmd.Parameters.AddWithValue("@Leather", row.Cells[3].Value.ToString());
cmd.Parameters.AddWithValue("@Colour", row.Cells[4].Value.ToString());
cmd.Parameters.AddWithValue("@unit_price", row.Cells[5].Value.ToString());
cmd.Parameters.AddWithValue("@Quantity", row.Cells[6].Value.ToString());
cmd.Parameters.AddWithValue("@Total", row.Cells[7].Value.ToString());
cmd.Parameters.AddWithValue("@Advancepayment", textBox4.Text);
cmd.Parameters.AddWithValue("@Duespayment", label35.Text);
cmd.ExecuteNonQuery();
}
}
to VB.NET :
For Each row As DataGridViewRow In dataGridView1.Rows
If row.Cells(0).Value Is Nothing AndAlso row.Cells(1).Value Is Nothing AndAlso row.Cells(2).Value Is Nothing AndAlso row.Cells(3).Value Is Nothing AndAlso row.Cells(4).Value Is Nothing AndAlso row.Cells(5).Value Is Nothing AndAlso row.Cells(6).Value Is Nothing AndAlso row.Cells(7).Value Is Nothing Then
Else
con.Open()
Dim cmd As New OleDbCommand("insert into Sold(Product_name,Model_no,Seater,Leather,Colour,Unit_price,Quantity,Total,Advancepayment,Duespayment) values (@Product_name,@Model_no,@Seater,@Leather,@Colour,@Unit_price,@Quantity,@Total,@Advancepayment,@Duespayment)", con)
cmd.Parameters.AddWithValue("@Product_name", row.Cells(0).Value.ToString())
cmd.Parameters.AddWithValue("@Model_no", row.Cells(1).Value.ToString())
cmd.Parameters.AddWithValue("@Seater", row.Cells(2).Value.ToString())
cmd.Parameters.AddWithValue("@Leather", row.Cells(3).Value.ToString())
cmd.Parameters.AddWithValue("@Colour", row.Cells(4).Value.ToString())
cmd.Parameters.AddWithValue("@unit_price", row.Cells(5).Value.ToString())
cmd.Parameters.AddWithValue("@Quantity", row.Cells(6).Value.ToString())
cmd.Parameters.AddWithValue("@Total", row.Cells(7).Value.ToString())
cmd.Parameters.AddWithValue("@Advancepayment", textBox4.Text)
cmd.Parameters.AddWithValue("@Duespayment", label35.Text)
cmd.ExecuteNonQuery()
End If
Next