Hi if you want to enter data in datagridview after that you want to save the data you can use this code. You should change the value according to you need.
void SaveEach()
{
try
{
if (dataGridView1.Rows.Count > 1)
{
for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
{
string col1 = dataGridView1.Rows[i].Cells[0].Value.ToString();
string col2 = dataGridView1.Rows[i].Cells[1].Value.ToString();
string col3 = dataGridView1.Rows[i].Cells[2].Value.ToString();
string col4 = dataGridView1.Rows[i].Cells[3].Value.ToString();
string col5 = dataGridView1.Rows[i].Cells[4].Value.ToString();
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
string insert = "INSERT INTO InvoiceItems(InvoiceNo,SNo,Quantity,Rate,Description,Total) VALUES (@InvoiceNo,@SNo,@Quantity,@Rate,@Description,@Total)";
con.Open();
SqlCommand cmd = new SqlCommand(insert, con);
cmd.Parameters.AddWithValue("@InvoiceNo", txt_invoiceno.Text);
cmd.Parameters.AddWithValue("@SNo", col1.ToString());
cmd.Parameters.AddWithValue("Description", col2.ToString());
cmd.Parameters.AddWithValue("@Quantity", col3.ToString());
cmd.Parameters.AddWithValue("@Rate", col4.ToString());
cmd.Parameters.AddWithValue("Total", col5.ToString());
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
catch (Exception ex)
{
ex.Message.ToString();
}
}
Thanks