C# 乘以 2 数据网格从输入中查看列并将结果放在第三列中

我正在处理 POS winforms 应用程序,但遇到了麻烦,datagridview 是由用户输入填充的,通过条码扫描仪,然后用户调整数据网格视图内的数量,我想做的是什么时候用户更改 Quantity 的值(默认设置为 1)它会乘以第二列价格并在第三列中显示结果


A 列(输入的值)x B 列 = 结果到 C 列


我试过使用这个代码,但没有运气


            private void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)

            {



                for (int i = 0; i < dataGridView1.Rows.Count; i++)

                {

            int a =dataGridView1.Rows[i][0].value;

            int b =dataGridView1.Rows[i][1].value;


            int c = a*b;


            c=dataGridView1.Rows[i][2].value;



      }

它给了我错误 14 无法将 [] 索引应用于“System.Windows.Forms.DataGridViewRow”类型的表达式


茅侃侃
浏览 137回答 2
2回答

紫衣仙女

您需要使用类的Cells属性DataGridViewRow用:int a = dataGridView1.Rows[i].Cells[0].Value;int b = dataGridView1.Rows[i].Cells[1].Value;int c = a * b;dataGridView1.Rows[i].Cells[2].Value = c;请注意,我将最后一行更改dataGridView1.Rows[i].Cells[2].Value = c;为似乎与您的问题相匹配……您希望将答案存储在单元格中。

互换的青春

decimal s = Convert.ToDecimal(dataGridView_DaginaAdd_SellInovice.CurrentRow.Cells[10].Value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; decimal s1 = Convert.ToDecimal(dataGridView_DaginaAdd_SellInovice.CurrentRow.Cells[11].Value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; decimal s13 = s + s1; Convert.ToDecimal(dataGridView_DaginaAdd_SellInovice.CurrentRow.Cells[12].Value = s13);
打开App,查看更多内容
随时随地看视频慕课网APP