如果datagrid中已经存在一个值,那么如何在C#中增加DataGridView中的值

如果datagrid中已经存在一个值,那么如何在C#中增加DataGridView中的值,c#,winforms,datagridview,increment,setvalue,C#,Winforms,Datagridview,Increment,Setvalue,我有这个代码,它做了正确的事情,但是选择器停留在第一行,所以它增加了错误条目的数量 我想搜索datagrid,如果第一列上有匹配项,则增加单元格[6]。值(项目数),然后使用单元格[3]中的价格查找总数。值,然后将单元格[6]。值(总数)设置为这两个单元格的乘积 private void productCodeBox_TextChanged(object sender, EventArgs e)//Event that waits for a correct barcode_id to be

我有这个代码,它做了正确的事情,但是选择器停留在第一行,所以它增加了错误条目的数量

我想搜索datagrid,如果第一列上有匹配项,则增加单元格[6]。值(项目数),然后使用单元格[3]中的价格查找总数。值,然后将单元格[6]。值(总数)设置为这两个单元格的乘积

  private void productCodeBox_TextChanged(object sender, EventArgs e)//Event that waits for a correct barcode_id to be entered in
{
    string[] itemInfo;//defines a string to hold product information coming in from the API using the getProduct method
    int quantity = 1;//sets the initial quantity to 1 as a default
    try//tries to parse any input in until a correct product code is entered else carries on checking
    {
        itemInfo = getProduct(productCodeBox.Text);//initializes the array with the product code that matches a value in the API

            foreach (DataGridViewRow row in productList.Rows)//used to check if the item is in the data gridview and adds a qty instead of a whole new item
            {
                if (row.Cells[0].Value.ToString().Equals(itemInfo[0]))//checks if the product code is already in the datagridview
                {
                    productList.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                    //Increments the qty
                    int qty = (int)productList.Rows[productList.CurrentRow.Index].Cells[6].Value;//gets the current value of the quantity field in the DataGridView at the selected position
                    productList.Rows[productList.CurrentRow.Index].Cells[6].Value = qty + 1;//Increments the value of the the quantity column in the datagrid view but only the first value.

                    //Gets the total of that column
                    double price = (double)productList.Rows[productList.CurrentRow.Index].Cells[3].Value;

                    double total = price * qty;//calculates the product
                    productList.Rows[productList.CurrentRow.Index].Cells[7].Value = total;//is meant to get the price from the total column but nothing is displayed

                    itemInfo = null;//resets the array so that the item is not duplicated
                    productCodeBox.Clear();//clears the box to wait for the next input

                } 

            }
            productList.Rows.Add(new object[] { itemInfo[0], itemInfo[1], itemInfo[2], itemInfo[3], itemInfo[4], itemInfo[5], quantity });//inserts the new value if it does not already exist
        productCodeBox.Clear();//clears the text box to wait for more input

    }
    catch(Exception a)
    {

    }


}

看起来这对你有用:

var matchedRow = productList.Rows.OfType<DataGridViewRow>()
                            .FirstOrDefault(row=>row.Cells[0].Value != null &&
                                                 row.Cells[0].Value.Equals(itemInfo[0]));
if(matchedRow != null){
  int qty = (int)matchedRow.Cells[6].Value + 1;
  double price = (double)matchedRow.Cells[3].Value;
  matchedRow.Cells[7].Value = price * qty;
}
var matchedRow=productList.Rows.OfType()
.FirstOrDefault(行=>行.Cells[0]。值!=null&&
row.Cells[0].Value.Equals(itemInfo[0]);
if(matchedRow!=null){
整数数量=(整数)匹配的行单元格[6]。值+1;
double price=(double)matchedRow.Cells[3]。值;
matchedRow.Cells[7]。值=价格*数量;
}

当我现在尝试添加另一项时,它不会将其添加到datagridview?@RyanSeanWattrus此代码不会阻止您添加行,您可以像往常一样自由添加它。例外情况是什么?或者干脆不添加行?你试过调试吗?只需将断点放在行
productList.Rows.Add(new object[…)
我现在正在使用调试,由于某种原因,它被困在循环中。数量正在增加,但未更新datagridview?我认为这是因为它总是返回null,因为在放入第一个值后没有更多匹配?@RyanSeanWattrus What循环?我的代码不使用任何循环,你需要删除你的循环并使用我的代码。哦,对不起,我仍然在使用foreach循环。它现在正在工作,但没有更新总数?如果您不想使用线程来永久性地添加内容,那么这非常有用