简体   繁体   English

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

[英]C# Multiply 2 Data Grid View columns from input and place result in third column

I'm working on a POS winforms application, and I'm having trouble, the datagridview is filled by user input, through a barcode scanner, and then the user adjusts the Quantity inside the data grid view, what I want to do is when the user changes the value of Quantity ( which is set to 1 by default) it gets multiplied by second column which is price and show the result in the third column我正在处理 POS winforms 应用程序,但遇到了麻烦,datagridview 是由用户输入填充的,通过条码扫描仪,然后用户调整数据网格视图内的数量,我想做的是什么时候用户更改 Quantity 的值(默认设置为 1)它会乘以第二列价格并在第三列中显示结果

Column A (value entered) x Column B = Result to Column C A 列(输入的值)x B 列 = 结果到 C 列

I've tried using this code, but no luck我试过使用这个代码,但没有运气

            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;


      }

it gave me Error 14 Cannot apply indexing with [] to an expression of type 'System.Windows.Forms.DataGridViewRow'它给了我错误 14 无法将 [] 索引应用于“System.Windows.Forms.DataGridViewRow”类型的表达式

You need to use the Cells property of the DataGridViewRow class您需要使用DataGridViewRow类的Cells属性

Use:用:

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;

Please note, I change the last line to be dataGridView1.Rows[i].Cells[2].Value = c;请注意,我将最后一行更改为dataGridView1.Rows[i].Cells[2].Value = c; as that seems to match your question...you want the answer stored in the cell.因为这似乎符合您的问题...您希望答案存储在单元格中。

            decimal s = Convert.ToDecimal(dataGridView_DaginaAdd_SellInovice.CurrentRow.Cells[10].Value);
            decimal s1 = Convert.ToDecimal(dataGridView_DaginaAdd_SellInovice.CurrentRow.Cells[11].Value);
            decimal s13 = s + s1; Convert.ToDecimal(dataGridView_DaginaAdd_SellInovice.CurrentRow.Cells[12].Value = s13);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM