简体   繁体   English

如何在DataGridview中更改另一个单元格值时更改一个单元格的值

[英]How to change value of one cell onchange of other cell value in DataGridview

例如:如果我要更改MRP列,则应将该值与另一个名为No_of_Units列值相乘,并且结果应存储在名为Total列中,谢谢

You mean something like this ? 你的意思是这样的吗?

private void DataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == index of column MRP)
    {
        int value = (int)DataGridView1.Rows[e.RowIndex].Cells["NO_OF_UNITS"].Value * (int)DataGridView1.Rows[e.RowIndex].Cells["MRP"].Value;
        DataGridView1.Rows[e.RowIndex].Cells["TARGET_COLUMN"].Value = value;
    }
}

This answer is simplified, you might need some additional checks, like check on null values and that kind of stuff. 该答案得到了简化,您可能需要一些其他检查,例如检查空值和类似的东西。 But it should get you on your way. 但这应该可以帮助您。

In your comment you asked: 在您的评论中,您询问:

How to get the index of column MRP ? 如何获取MRP列的索引?

Click on the datagrid. 单击数据网格。 Find the property Columns and click on the little button in the list of columns, find the column MRP and copy the Name property. 找到“列”属性,然后单击列列表中的小按钮,找到列MRP并复制Name属性。 This could be MRP but it also be DataGridColumn1 or something. 这可以是MRP,但也可以是DataGridColumn1之类的。

Suppose the name property is 'MRP' than you can do: 假设name属性是您可以做的'MRP':

if (e.ColumnIndex == MRP.ColumnIndex)

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

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