如何在 C# 中编辑/更新 DataGridView 中的一行 [英] How to Edit/Update a row in DataGridView in C#

查看:106
本文介绍了如何在 C# 中编辑/更新 DataGridView 中的一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理类似 WF 中的 Form 之类的购物车.我有一个 DataGridView 一个 ADD_ButtonSubmit_Button.用户将从库存中选择项目并点击 ADD_Button 项目将进入 DataGridView 完成后用户将点击 Submit_Button 然后详细信息将进入数据库.

I m working on a shopping cart like Form in WF. I have a DataGridView an ADD_Button and Submit_Button.The user will choose Items From inventory and Click ADD_ButtonThe item will go into DataGridView After Finishing User will click Submit_Button then detail will go into DB.

问题:这是在将产品/行添加到 DatagridView 之后,当我再次添加相同的产品时,它会进入我想要的新行,其中 Pro_ID 列匹配,用新数量更新行.我试图在网上搜索,但我得到的都是 SQL 查询.

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. I tried to search the web but all I got SQL queries.

 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 ){}

     }

这是提交按钮代码

private void btnInsert_Click(object sender, EventArgs e){string cs = ConfigurationManager.ConnectionStrings["PRMSConnectionString"].ToString();SqlConnection con = new SqlConnection(cs);SqlTransaction objTransaction;

private void btnInsert_Click(object sender, EventArgs e) { string cs = ConfigurationManager.ConnectionStrings["PRMSConnectionString"].ToString(); SqlConnection con = new SqlConnection(cs); SqlTransaction objTransaction;

        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) {}
         } 
     }

推荐答案

试试这个:

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;
    }
}

这篇关于如何在 C# 中编辑/更新 DataGridView 中的一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆