VB.NET:DataGridView同一列中的不同按钮

时间:2018-08-30 00:50:26

标签: vb.net

我正试图初始化一个检查器,该检查器检查datagridview中特定列的单元格,如果该单元格等于StaffMixname,则按钮文本应为VIEW;否则,按钮文本为LOCKED

到目前为止,这是我的代码。

Dim dgButtonColumn As New DataGridViewButtonColumn
Dim i As Integer
MetroGrid7.Columns.Add(dgButtonColumn)
dgButtonColumn.HeaderText = "Security"
dgButtonColumn.UseColumnTextForButtonValue = True
For i = 0 To MetroGrid7.Rows.Count
    If MetroGrid7.Rows(i).Cells.Item(4).Value.ToString() = StaffMixname.Text Then
       dgButtonColumn.Text = "VIEW"
       dgButtonColumn.Name = "viewBtn"
       dgButtonColumn.ToolTipText = "View"
    Else
       dgButtonColumn.Text = "LOCKED"
       dgButtonColumn.Name = "searchSecurityBtn"
       dgButtonColumn.ToolTipText = "LOCKED"
    End If
Next

PEP 3119

我想要的结果是这样的 enter image description here

2 个答案:

答案 0 :(得分:1)

它不能像这样工作。在DataGridViewButtonColumn中,每个单元格都包含一个button,但您不能访问它。您可以获取DataGridViewButtonCell并更改这两个属性value and ToolTipTextname中没有DataGridViewButtonCell属性。所以要更改它们:

Dim dgButtonColumn As New DataGridViewButtonColumn
Dim i As Integer
MetroGrid7.Columns.Add(dgButtonColumn)
dgButtonColumn.HeaderText = "Security"
'remove this line
'dgButtonColumn.UseColumnTextForButtonValue = True
For i = 0 To MetroGrid7.Rows.Count
    If MetroGrid7.Rows(i).Cells.Item(4).Value.ToString() = StaffMixname.Text Then
       MetroGrid7.Rows(i).Cells.Item(6).Value = "VIEW"
       MetroGrid7.Rows(i).Cells.Item(6).ToolTipText = "View"
    Else
       MetroGrid7.Rows(i).Cells.Item(6).Value = "LOCKED"
       MetroGrid7.Rows(i).Cells.Item(6).ToolTipText = "LOCKED"
   End If
Next

答案 1 :(得分:0)

Dim status As String
status = status & Me.dgTitleList.CurrentRow.Cells("BDO").Value
status = status & Chr(13)

Value Datagridview的{​​{1}}插入Column并满足条件。

String

我建议使用BreakPoint并检查If status = StaffMixname.Text Then dgButtonColumn.Text = "VIEW" dgButtonColumn.Name = "viewBtn" dgButtonColumn.ToolTipText = "View" Else dgButtonColumn.Text = "LOCKED" dgButtonColumn.Name = "searchSecurityBtn" dgButtonColumn.ToolTipText = "LOCKED" End If 是否具有StaffMixName.Text