Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DataGridView仅编辑列中的一行_C#_Datagridview - Fatal编程技术网

C# DataGridView仅编辑列中的一行

C# DataGridView仅编辑列中的一行,c#,datagridview,C#,Datagridview,我有一个家庭作业,我几乎完成了,但我不能得到这一个 这是我的数据网格视图列 Name , Price , Quantity , Total , button(to increase) , button (to decrease) 在数据网格视图中,当我双击增加按钮时,数量将每次增加1 所以我做了这个代码 foreach (DataGridViewRow row in dataGridView1.Rows) { row.Cells["Qty"].Value

我有一个家庭作业,我几乎完成了,但我不能得到这一个 这是我的数据网格视图列

Name , Price , Quantity , Total , button(to increase) , button (to decrease)
在数据网格视图中,当我双击增加按钮时,数量将每次增加1 所以我做了这个代码

foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            row.Cells["Qty"].Value = Convert.ToDouble(row.Cells["Add"].Value) + Convert.ToDouble(row.Cells["Qty"].Value) + 1;
        }
但是如果我双击它时有两行,它会覆盖整个列,两行的数量都在增加
我如何才能将按钮的代码更改为仅1行

您正在使用for语句遍历所有行

您应该使用
CurrentCell.RowIndex
属性来获取当前选定的行

因此,您的代码应该如下所示:

var row = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex];
row.Cells["Qty"].Value = Convert.ToDouble(row.Cells["Add"].Value) + Convert.ToDouble(row.Cells["Qty"].Value) + 1;

您正在使用for语句遍历所有行

您应该使用
CurrentCell.RowIndex
属性来获取当前选定的行

因此,您的代码应该如下所示:

var row = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex];
row.Cells["Qty"].Value = Convert.ToDouble(row.Cells["Add"].Value) + Convert.ToDouble(row.Cells["Qty"].Value) + 1;

问题出在前面。
在按钮事件中,您需要获取行索引。
您可以使用

 var index = datagridview.CurrentCell.RowIndex;
然后你可以这样做:

dataGridView1.Rows[index].Cells["Qty"].Value = 
Convert.ToDouble(dataGridView1.Rows[index].Cells["Add"].Value) 
+ Convert.ToDouble(dataGridView1.Rows[index].Cells["Qty"].Value) + 1;

问题出在前面。
在按钮事件中,您需要获取行索引。
您可以使用

 var index = datagridview.CurrentCell.RowIndex;
然后你可以这样做:

dataGridView1.Rows[index].Cells["Qty"].Value = 
Convert.ToDouble(dataGridView1.Rows[index].Cells["Add"].Value) 
+ Convert.ToDouble(dataGridView1.Rows[index].Cells["Qty"].Value) + 1;

如果您仔细想想,代码正按照您的要求执行。你有一个for-each循环,在这里你通过,每个数量有一个for-each循环。相反,您需要过滤单击的行以向其中添加一行。尝试使用编码到每个按钮中的索引,只在单击相应的按钮时增加该行的编号。@谢谢帮助:)我对这类工作还是新手,所以我一直无法理解。再次感谢帮助:)如果你仔细想想,代码正是按照你告诉它的做的。你有一个for-each循环,在这里你通过,每个数量有一个for-each循环。相反,您需要过滤单击的行以向其中添加一行。尝试使用编码到每个按钮中的索引,只在单击相应的按钮时增加该行数。@bw谢谢帮助:)我对这类工作还是新手,所以我一直无法理解。再次感谢帮助:)