[英]How to Edit/Update a row in DataGridView in C#
I m working on a shopping cart like Form in WF.我正在处理像 WF 中的 Form 这样的购物车。 I have a
DataGridView
an ADD_Button
and Submit_Button
.The user will choose Items From inventory and Click ADD_Button
The item will go into DataGridView
After Finishing User will click Submit_Button
then detail will go into DB.我有一个
DataGridView
一个ADD_Button
和Submit_Button
。用户将从库存中选择项目并单击ADD_Button
该项目将在完成后进入DataGridView
用户将单击Submit_Button
然后详细信息将进入数据库。
Question: is this After adding a product/row into DatagridView
When I add same product again.it goes into the new row I want that Where Pro_ID
Column Match, The row update with new qty.问题:这是在将产品/行添加到
DatagridView
吗?当我再次添加相同的产品时,它会进入我想要的新行,其中Pro_ID
列匹配,该行更新为新数量。 I tried to search the web but all I got SQL queries.我试图搜索网络,但我得到的都是 SQL 查询。
private void btn_Add_Click(object sender, EventArgs e)
{
i = dgv_Purchase.Rows.Count;
try
{
dgv_Purchase.Rows.Add();
.......
.......
dgv_Purchase.Rows[i - 1].Cells["Pro_ID"].Value = txt_ProID.Text;
.......
.......
dgv_Purchase.Rows[i - 1].Cells["Purchase_Qty"].Value = txt_Qty.Text;
}
catch (Exception ){}
}
This is Submit Button Code这是提交按钮代码
private void btnInsert_Click(object sender, EventArgs e) { string cs = ConfigurationManager.ConnectionStrings["PRMSConnectionString"].ToString(); private void btnInsert_Click(object sender, EventArgs e) { string cs = ConfigurationManager.ConnectionStrings["PRMSConnectionString"].ToString(); SqlConnection con = new SqlConnection(cs);
SqlConnection con = new SqlConnection(cs); SqlTransaction objTransaction;
SqlTransaction 对象事务;
for (int i = 0; i < dgv_Purchase.Rows.Count - 1; i++)
{
//SomeCode part of code
SqlCommand objCmd2;
string cmd2 = "INSERT INTO PurchaseMaster " +
" (Pro_ID , category_ID, Purchase_Qty) " +
"VALUES (@Pro_ID, @category_ID, @Purchase_Qty)";
objCmd2 = new SqlCommand(cmd2, con, objTransaction);
objCmd2.Parameters.AddWithValue("@Pro_ID_ID", dgv_Purchase.Rows[i].Cells["Pro_ID"].Value.ToString());
objCmd2.Parameters.AddWithValue("@Category_ID", dgv_Purchase.Rows[i].Cells["Category_ID"].Value.ToString());
objCmd2.Parameters.AddWithValue("@Purchase_Qty", Convert.ToInt32(dgv_Purchase.Rows[i].Cells["Purchase_Qty"].Value.ToString()));
objCmd2.Parameters.AddWithValue("@Date_Today", Convert.ToDateTime(dgv_Purchase.Rows[i].Cells["Purchase_Date"].Value.ToString()));
...........................
Rest of the Code
...........................
try
{
objCmd2.ExecuteNonQuery();
objTransaction.Commit();
}
catch (Exception) {}
}
}
I edit it with a DGVrow instead of DataRow我用 DGVrow 而不是 DataRow 编辑它
foreach (DataGridViewRow dr in dataGridView1.Rows)
{
if (dr.Cells["Pro_ID"].Value.ToString() == txt_ProID.Text)
{
dr.Cells["Purchase_Qty"].Value = txt_Qty.Text;
}
}
Try this:尝试这个:
private void AddInfo()
{
// flag so we know if there was one dupe
bool updated = false;
// go through every row
foreach (DataGridViewRow row in dgv_Purchase.Rows)
{
// check if there already is a row with the same id
if (row.Cells["Pro_ID"].ToString() == txt_ProID.Text)
{
// update your row
row.Cells["Purchase_Qty"] = txt_Qty.Text;
updated = true;
break; // no need to go any further
}
}
// if not found, so it's a new one
if (!updated)
{
int index = dgv_Purchase.Rows.Add();
dgv_Purchase.Rows[index].Cells["Purchase_Qty"].Value = txt_Qty.Text;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.