gpt4 book ai didi

mysql - 如何禁用或隐藏 DataGridView 中的按钮

转载 作者:行者123 更新时间:2023-11-30 22:03:47 25 4
gpt4 key购买 nike

您好,我想隐藏或禁用第 3 列中的按钮,我打算在每次单击它时添加行。乱七八糟的是,以前的按钮是事件的,可以添加行。如何将按钮仅放在最后一行?

这是我的工作代码:

    Private Sub dgAppliances_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgAppliances.CellContentClick
If e.ColumnIndex <> 2 Then
Exit Sub
Else
If Me.dgAppliances.RowCount - 1 = 20 Then
MsgBox("Maximum of 20 appliances only.")
Else
Me.dgAppliances.Rows.Add()
End If
End If
End Sub

但是当我添加一些代码时:

    Private Sub dgAppliances_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgAppliances.CellContentClick
If e.ColumnIndex <> 2 Then
Exit Sub
Else
If Me.dgAppliances.RowCount - 1 = 20 Then
MsgBox("Maximum of 20 appliances only.")
Else
Me.dgAppliances.Rows.Add()
Dim cell As DataGridViewButtonCell = dgAppliances.Rows(0).Cells(2)
cell.Value = String.Empty
cell = New DataGridViewColumn()
cell.ReadOnly = True
End If
End If
End Sub

这行 cell = New DataGridViewColumn() 有错误。

它说,“System.Windows.Forms.DataGridViewColumn”无法转换为“System.Windows.Forms.DataGridViewButtonCell”。

谁能帮我解决这个问题? TIA。

Here's the sample image.

最佳答案

您正在尝试将 DataGridViewColumn 分配给 DataGridViewButtonCell - 将整个分配给单个单元格 .

也许您的意图是:cell = New DataGridViewTextBoxCell()?但即便如此,如果你想

place the button only in the last row

然后你会遇到Adding Different DataGridView Cell Types to a Column的问题.您可以如下所示进行转换:

Me.dataGridView1.ColumnCount = 3
Me.dataGridView1.AllowUserToAddRows = False
Me.dataGridView1.AllowUserToDeleteRows = False
Me.dataGridView1.RowCount = 1
Me.dataGridView1(2, 0) = New DataGridViewButtonCell()

Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dataGridView1.CellContentClick
If TypeOf Me.dataGridView1(e.ColumnIndex, e.RowIndex) Is DataGridViewButtonCell Then
If Me.dataGridView1.RowCount - 1 = 20 Then
MessageBox.Show("Maximum of 20 appliances only.")
Else
Me.dataGridView1.Rows.Add()
Me.dataGridView1(e.ColumnIndex, e.RowIndex) = New DataGridViewTextBoxCell()
Me.dataGridView1(e.ColumnIndex, e.RowIndex).[ReadOnly] = True
Me.dataGridView1(e.ColumnIndex, Me.dataGridView1.RowCount - 1) = New DataGridViewButtonCell()
End If
End If
End Sub

关于mysql - 如何禁用或隐藏 DataGridView 中的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42430717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com