候我想隐藏在DataGridViewButtonColumn按钮

候我想隐藏在DataGridViewButtonColumn按钮

本文介绍了有时候我想隐藏在DataGridViewButtonColumn按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DataGridView的这是前一个问题的主题(的)。但有时按钮。这可以。但如果是空,有什么办法,我可以有选择地删除/添加(显示/隐藏?)按钮在 DataGridViewButtonColumn 按钮



是这样的:

  + ------------ + --- --------- + 
| MYTEXT |用作MyButton |
+ ------------ + ------------ +
| 这样做| (是)|
| 做| (是)|
| FYI'嗒嗒'| | < ----这是我想可选没有按钮
| 做其他| (是)|
+ ------------ + ------------ +

这是我到目前为止已经试过(的)

 私人无效grdVerdict_CellFormat(对象发件人,DataGridViewCellFormattingEventArgs E)
{
如果(e.ColumnIndex == grdChoice.Columns [yesbutton]。指数)
{
如果(grdVerdict [e.ColumnIndex,e.RowIndex] .value的== NULL)
{
//grdVerdict[e.ColumnIndex,e.RowIndex]。可见=虚假的; //< -says'这是只读'
//grdVerdict[e.ColumnIndex,e.RowIndex] .value的=新的DataGridTextBox(); //< - 画疯狂红十字会在全网
//((Button)grdVerdict[e.ColumnIndex,e.RowIndex])隐藏; //< - 将无法正常工作
}
,否则
{
e.Value =((按钮)grdChoice [e.ColumnIndex,e.RowIndex] .value的)。文本;
}
}
}


解决方案

今天我有同样的问题。我也想隐藏某些行的按钮。与玩了一段时间后,我发现了一个非常简单和很好的解决方案,即不需要任何重载的paint() -functions或类似的东西:



只是分配一个不同的的DataGridViewCellStyle 这些细胞。结果
的关键是,你将填充的这种新款式到转移全键出细胞的可见区域的价值属性。结果
这是它! : - )



示例:

  VAR dataGridViewCellStyle2 =新的DataGridViewCellStyle {填充=新填充(100,0,0,0)}; 
cell.Style = dataGridViewCellStyle2;

// 0列的宽度为22
//,而不是固定25,你可以使用`columnWidth时+ 1`也。


I have a DataGridView which was the subject of a previous question (link). But sometimes the Button is null. This is fine. But if it is null, is there any way I can optionally remove/add (show/hide?) buttons to the DataGridViewButtonColumn of Buttons

like this:

+------------+------------+
| MyText     | MyButton   |
+------------+------------+
| "do this"  | (Yes)      |
| "do that"  | (Yes)      |
| FYI 'blah' |            | <---- this is where I optionally want no button
| "do other" | (Yes)      |
+------------+------------+

this is what I have tried so far (based on this example)

private void grdVerdict_CellFormat(object sender, DataGridViewCellFormattingEventArgs e)
{
   if (e.ColumnIndex == grdChoice.Columns["yesbutton"].Index)
   {
       if (grdVerdict[e.ColumnIndex, e.RowIndex].Value == null)
       {
            //grdVerdict[e.ColumnIndex, e.RowIndex].Visible = false; //<-says 'it is read only'
            //grdVerdict[e.ColumnIndex, e.RowIndex].Value = new DataGridTextBox(); //<- draws 'mad red cross' over whole grid
            //((Button)grdVerdict[e.ColumnIndex, e.RowIndex]).Hide; //<- won't work
       }
       else
       {
          e.Value = ((Button)grdChoice[e.ColumnIndex, e.RowIndex].Value).Text;
       }
   }
}
解决方案

I had the same "problem" today. I also wanted to hide buttons of certain rows. After playing around with it for a while, I discovered a very simple and nice solution, that doesn't require any overloaded paint()-functions or similar stuff:

Just assign a different DataGridViewCellStyle to those cells.
The key is, that you set the padding property of this new style to a value that shifts the whole button out of the visible area of the cell.
That's it! :-)

Sample:

var dataGridViewCellStyle2 = new DataGridViewCellStyle { Padding = new Padding(100, 0, 0, 0) };
cell.Style = dataGridViewCellStyle2;

// The width of column 0 is 22.
// Instead of fixed 25, you could use `columnwidth + 1` also.

这篇关于有时候我想隐藏在DataGridViewButtonColumn按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 01:17