如何从datagridview的多行更新表。我的代码有效但只更新了我的datagridview的最后一行而不是所有行。 [英] How to update table from multiple rows of a datagridview. My code works but only updates the last line of my datagridview not all lines.
本文介绍了如何从datagridview的多行更新表。我的代码有效但只更新了我的datagridview的最后一行而不是所有行。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
cn.Open()
cmd.CommandText = "INSERT INTO CoursesLines (FieldID, CoursID, CoursDescr) VALUES (@FieldID2, @CourseID, @Descr)"
cmd.Parameters.AddWithValue("@FieldID2", Me.DataGridView1.Item(0, Me.DataGridView1.CurrentRow.Index).Value)
cmd.Parameters.AddWithValue("@CourseID", Me.DataGridView1.Item(1, Me.DataGridView1.CurrentRow.Index).Value)
cmd.Parameters.AddWithValue("@Descr", Me.DataGridView1.Item(2, Me.DataGridView1.CurrentRow.Index).Value)
cmd.ExecuteNonQuery()
cn.Close()
推荐答案
你可以使用 SqlDataAdapter
SqlDataAdapter Class
SqlDataAdapter.UpdateCommand Property
// this is helping code to please check.
class CodeProject_Solution
{
SqlConnection con = new SqlConnection("your constring");
SqlCommand cmd = new SqlCommand("select * form CoursesLines");
DataTable dtTemp = new DataTable();
private void UpdateSolution()
{
//open connection
con.Open();
cmd.Connection = con;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
builder.ConflictOption = ConflictOption.OverwriteChanges;//to insert update and delete data and also update dtTemp
adapter.Fill(dtTemp);
}
private void AddMultiValues()
{
for (int i = 0; i < 50; i++)
{
DataRow dr = dtTemp.NewRow();
dr["FId"] = "id" + i;
dr["CoursID"] = "CoursID" + i;
dr["CoursDescr"] = "CoursDescr" + i;
dtTemp.Rows.Add(dr);
}
dtTemp.AcceptChanges();
//know call function
UpdateSolution();
//know all 50 rows will be inserted in db. and also update DataTable and CoursesLines(in database)
}
}
如果有任何查询请告诉我。
谢谢。
帮助链接。
使用DataAdapters更新数据源
>
这篇关于如何从datagridview的多行更新表。我的代码有效但只更新了我的datagridview的最后一行而不是所有行。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文