如何将dataGridView中的记录更新到数据库? [英] how to update records from dataGridView to database ?
本文介绍了如何将dataGridView中的记录更新到数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在google上搜索但我无法为我的项目找到正确的答案..
我创建了一个表单,我希望通过dataGridView插入,更新和删除记录。 。
我的代码是: -
I searched on google but I couldn''t get the right answer for my project..
I create a form in which i want to Insert,update and delete the records through the dataGridView..
My code is:-
public partial class orderListDetails : Form
{
Data_Conn DC;
string DelValue = string.Empty;
public orderListDetails()
{
InitializeComponent();
DC = new Data_Conn(Application.StartupPath);
}
private void orderListDetails_Load(object sender, EventArgs e)
{
DC.SelectSta("select proName as Name,proComp as Company,proQty as Qty,proUnit as Unit from orderList", "orderList");
dataGridView1.DataSource = DC.dataSet;
dataGridView1.DataMember = "orderList";
}
private void button5_Click(object sender, EventArgs e)
{
this.Dispose();
}
private void btnAddNew_Click(object sender, EventArgs e)
{
DC.CmdStatement("insert into orderList values('" + textBox1.Text + "','" + comboBox1.Text + "'," + Convert.ToInt32(textBox2.Text) + ",'" + comboBox2.Text + "')");
DC.SelectSta("select proName as Name,proComp as Company,proQty as Qty,proUnit as Unit from orderList", "orderList");
dataGridView1.DataSource = DC.dataSet;
dataGridView1.DataMember = "orderList";
dataGridView1.Refresh();
}
private void btnDelete_Click(object sender, EventArgs e)
{
try
{
DialogResult dr = MessageBox.Show("Do You want to delete the product details?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
DC.CmdStatement("delete from orderList where proName='"+DelValue+"'");
DC.SelectSta("select proName as Name,proComp as Company,proQty as Qty,proUnit as Unit from orderList", "orderList");
dataGridView1.DataSource = DC.dataSet;
dataGridView1.DataMember = "orderList";
}
}
catch
{ }
}
private void btnUpdate_Click(object sender, EventArgs e)
{
DC.adpt.Update(DC.dataSet, "orderList");
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
DelValue = dataGridView1[0, e.RowIndex].Value.ToString();
}
}
点击更新按钮时显示错误消息
when i click on Update button Its shows the error message
Update requires a valid UpdateCommand when passed DataRow collection with modified rows.
如何在数据库中更新该记录?
感谢提前....
How to Update that record in database??
thanks in Advance....
推荐答案
正如ThePhantomUpvoter如此雄辩地说,你必须按顺序编写一个Update语句更新数据库。在网上有无数的例子。
As ThePhantomUpvoter so eloquently put it, you must write an Update statement in order to update a database. Ther are countless examples of them online.
你需要使用<$ href =http://中描述的 OleDbCommandBuilder
msdn.microsoft.com/en-us/library/system.data.common.dataadapter.update.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2\">这个例子 [ ^ ]。
You need to use the OleDbCommandBuilder
as described in this example[^].
这篇关于如何将dataGridView中的记录更新到数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文