[英]How to update Access database using a datagridview in C#
I have a datagridview
that bind with a datatable
and using DataPropertyName
the datagridview
only shows 4 columns of database
. 我有一个datagridview
,与一个绑定datatable
,并使用DataPropertyName
的datagridview
只能显示4列database
。
this is how I bind database
to a datagridview
: 这就是我将database
绑定到datagridview
:
string StrCon = System.Configuration.ConfigurationManager.ConnectionStrings
["FeedLibraryConnectionString"].ConnectionString;
OleDbConnection Connection = new OleDbConnection(StrCon);
OleDbDataAdapter DataA = new OleDbDataAdapter("Select * from SelectedFeeds", Connection);
DataTable DTable = new DataTable();
DataA.Fill(DTable);
frm.RationFeedsdataGridView.AutoGenerateColumns = false;
frm.RationFeedsdataGridView.Columns[1].Name = "نام خوراک";
frm.RationFeedsdataGridView.Columns[1].HeaderText = "نام خوراک";
frm.RationFeedsdataGridView.Columns[1].DataPropertyName = "Feed Name / Description";
frm.RationFeedsdataGridView.Columns[2].Name = "مقدار (کیلوگرم)";
frm.RationFeedsdataGridView.Columns[2].HeaderText = "مقدار (کیلوگرم)";
frm.RationFeedsdataGridView.Columns[2].DataPropertyName = "Quantity";
frm.RationFeedsdataGridView.Columns[3].Name = "درصد (خوراک)";
frm.RationFeedsdataGridView.Columns[3].HeaderText = "درصد (خوراک)";
frm.RationFeedsdataGridView.Columns[3].DataPropertyName = "Percent";
frm.RationFeedsdataGridView.DataSource = DTable;
So far nothing wrong, 到目前为止没有错
now I want when user change the value of Columns[2]
, it's value automatically inserted (and after that updated) into database
I tryed myself using this code but seems that it's wrong, the code is: 现在我想当用户更改Columns[2]
的值时,它的值自动插入(并在更新后)到database
我尝试使用此代码进行尝试,但似乎不对,代码为:
string StrCon = System.Configuration.ConfigurationManager.ConnectionStrings
["FeedLibraryConnectionString"].ConnectionString;
OleDbConnection Connection = new OleDbConnection(StrCon);
OleDbCommand Cmd = new OleDbCommand();
Cmd.Connection = Connection;
double Quantity = Convert.ToDouble(frm.RationFeedsdataGridView.CurrentCell.Value);
string FeedName = frm.RationFeedsdataGridView.CurrentRow.Cells[1].Value.ToString();
Connection.Open();
Cmd.CommandText="Update SelectedFeeds set Quantity=" +Quantity 'Where Feed Name / Description'= FeedName;
Cmd.ExecuteNonQuery();
Connection.Close();
the first problem is that my CommandText is wrong and I don't know how to fix it. 第一个问题是我的CommandText错误,我不知道如何解决。 what should I do? 我该怎么办?
tanks to @LarsTech the answer is: 坦克@LarsTech的答案是:
public static void UpdateRationFeeds(Form1 frm)
{
string StrCon = System.Configuration.ConfigurationManager.ConnectionStrings["FeedLibraryConnectionString"].ConnectionString;
OleDbConnection Connection = new OleDbConnection(StrCon);
OleDbCommand Cmd = new OleDbCommand();
Cmd.Connection = Connection;
double Quantity = Convert.ToDouble(frm.RationFeedsdataGridView.CurrentCell.Value);
string FeedName = frm.RationFeedsdataGridView.CurrentRow.Cells[1].Value.ToString();
Connection.Open();
Cmd.CommandText = "Update SelectedFeeds set Quantity=@quantity Where SelectedFeeds.[Feed Name]=@feedname";
Cmd.Parameters.Add("@quantity", OleDbType.Double).Value = Quantity;
Cmd.Parameters.Add("@feedname", OleDbType.LongVarChar).Value = FeedName;
Cmd.ExecuteNonQuery();
Connection.Close();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.