简体   繁体   English

在datagridview中向某个单元格添加按钮

[英]Add button to certain cell in datagridview

I have created a Datagridview. 我创建了一个Datagridview。 Now whenever the cell contains an empty value, It will add the plus button to the cell. 现在,只要单元格包含空值,它就会将加号按钮添加到单元格中。 Something like this: 像这样的东西:

if ((string)((Hashtable)ht[i])["value"] == "")
  {
     // Create a Save button column
     DataGridViewImageButtonSaveColumn columnSave = new DataGridViewImageButtonSaveColumn();

     // Set column values

     //Add the columns to the grid6
     if (!dataGridView1.Columns.Contains("SaveButton"))
        {
           dataGridView1.Columns.Insert(5, columnSave);
        }
  }
  else
  {
     //hide save button.
  }

But now my problem is that it display the plus button at all cell for that column even though that particular cell have value. 但现在我的问题是它在该列的所有单元格上显示加号按钮,即使该特定单元格具有值。 How can I hide the button for the cell that have value? 如何隐藏具有值的单元格的按钮?

If you only want buttons in some rows then I would tend not to use a DataGridViewButtonColumn . 如果您只想要某些行中的按钮,那么我倾向于不使用DataGridViewButtonColumn I would use a read-only DataGridViewTextBoxColumn , so that it displays empty cells by default. 我会使用只读的DataGridViewTextBoxColumn ,因此它默认显示空单元格。 For those rows where you want a button, you would then create a DataGridViewButtonCell and use the indexer of the grid itself to place that cell at a specific column and row index. 对于那些需要按钮的行,然后创建一个DataGridViewButtonCell并使用网格本身的索引器将该单元格放在特定的列和行索引处。

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

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