根据DataGridView中的值隐藏/禁用DataGridViewButton [英] Hide/Disable DataGridViewButton based on value in DataGridView
问题描述
所以,我有一个带有datagridview的表单。我创建了一个按钮列,在click事件中,我打开了datagridview中该特定行的编辑表单。最后一列是一个复选框字段,我想触发数据网格视图中按钮的隐藏/禁用。我尝试了很多选项,比如冻结,可见,而且没有任何地方。我注意到在c#中有一个findcontrols的选项,这在VB.net中是不可用的。
有没有办法做到这一点?能给我举个例子?
下面列出的代码就是我在表单加载后清理行时调用的内容。
公共 功能 clean_up_rows( )
对于 每个行 As DataGridViewRow 在 DGPOSTINGS.Rows
If (IsDBNull(row.Cells( 15 )。Value))然后
' MsgBox(row.Cells(0).ToString() )
row。[ ReadOnly ] = False
行。 DefaultCellStyle.BackColor = Color.DarkGray
ElseIf row.C ells( 15 )。Value.ToString = True 然后
' MsgBox(row.Cells(0).ToString())
' Dim cell As DataGridViewButtonCell = row.Cells(0)
row.Cells( 0 )。Visible = False
' row.DefaultCellStyle.BackColor = Color.DarkGray
结束 如果
下一步
返回 0
结束 功能
如果 conditionToDisable 然后
Dim cell As 新 DataGridViewTextBoxCell ' 替换TextCell的ButtonCell'
cell.Value = valueForCell ' 再次设置值'
grid.Rows(r ).Cells(c)= cell ' 覆盖单元格
结束 如果
So, I have a form with a datagridview. I created a button column that on the click event, I open up an edit form for that particular row in the datagridview. The last column is a check box field that I want to trigger a hide/disable of the button in the datagridview. I have tried numerous options like freeze, visable, and not getting anywhere. I noticed in c# there is an option for findcontrols which isn't available to me in VB.net.
Is there any way to do this? Can you give me an example?
The code listed below is what I call after I do the form load to clean up the rows.
Public Function clean_up_rows()
For Each row As DataGridViewRow In DGPOSTINGS.Rows
If (IsDBNull(row.Cells(15).Value)) Then
' MsgBox(row.Cells(0).ToString())
row.[ReadOnly] = False
row.DefaultCellStyle.BackColor = Color.DarkGray
ElseIf row.Cells(15).Value.ToString = True Then
' MsgBox(row.Cells(0).ToString())
'Dim cell As DataGridViewButtonCell = row.Cells(0)
row.Cells(0).Visible = False
'row.DefaultCellStyle.BackColor = Color.DarkGray
End If
Next
Return 0
End Function
I have gotten this to work, I am now wondering if there is a better way. Please see the code here:
If conditionToDisable Then Dim cell As New DataGridViewTextBoxCell 'Replace the ButtonCell for a TextCell' cell.Value = valueForCell 'Set the value again' grid.Rows(r).Cells(c) = cell 'Override the cell' End If
这篇关于根据DataGridView中的值隐藏/禁用DataGridViewButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!