简体   繁体   English

将所选单元格的值从datagridview更改为零(0)

[英]Change value of selected cell from datagridview to zero (0)

I have connection to an Excel file which displays in datagridview. 我已连接到显示在datagridview中的Excel文件。 I would like to change value of selected cell ind datagridview to "0" (zero) by clicking a button. 我想通过单击按钮将所选单元格ind datagridview的值更改为“ 0”(零)。

在此处输入图片说明 As you can see from pic, there is a button "Null cell" and selected one cell. 从图片中可以看到,有一个“空单元格”按钮,并且选中了一个单元格。 Any ideas? 有任何想法吗?

Try to use the code below. 尝试使用下面的代码。 If you will select several cells than the button sets the value to zero only for the first cell. 如果您将选择多个单元格,则该按钮仅将第一个单元格的值设置为零。

private void button1_Click(object sender, EventArgs e)
{
    var selected = dataGridView1.SelectedCells[0];
    dataGridView1.Rows[selected.RowIndex].Cells[selected.ColumnIndex].Value = 0;
}

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

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