如果已经在数据网格中存在,则如何在C#中的DataGridView中增加值

时间:2013-10-19 08:16:14

标签: c# winforms datagridview increment setvalue

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

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

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

    }


}

1 个答案:

答案 0 :(得分:1)

看起来这适合你:

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