繁体   English   中英

在Datagridview VB.NET中选择行时选中复选框列

[英]Make Checkbox Column Checked when Row Selected in Datagridview VB.NET

大家好,下午好。

我有个问题,

我在Datagridview中有Checkbox Column,并且该列为0。

在Datagridview中选择特定行时,如何使“复选框”列处于选中状态? 当选择一行时,请选中复选框列。

这是我的代码

将数据从数据库填充到Datagridview时连接到另一个代码的代码

 Dim checkBoxColumn As New DataGridViewCheckBoxColumn()
        checkBoxColumn.HeaderText = "Tag"
        checkBoxColumn.Width = 30
        checkBoxColumn.Name = "checkBoxColumn"
        DataGridView1.Columns.Insert(0, checkBoxColumn)

选择行时的代码。

 Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        If e.ColumnIndex = DataGridView1.Columns(0).Index Then
            DataGridViewCheckBoxColumn_Uncheck()
            Dim cell As DataGridViewCheckBoxCell = DataGridView1.Rows(e.RowIndex).Cells(0)
            cell.Value = cell.TrueValue
        End If
    End Sub
    Private Sub DataGridViewCheckBoxColumn_Uncheck()
        For Each row As DataGridViewRow In DataGridView1.Rows
            Dim cell As DataGridViewCheckBoxCell = row.Cells(0)
            cell.Value = cell.FalseValue
        Next
    End Sub

我的代码没有错误,但是有问题。 这是我真的需要选择“复选框”列并选中它,并且当“行选择”更改为“最后一个选定项”时未选中。

我希望你能得到我。

TYSM为将来提供帮助

尝试将您的代码更改为:

     Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

            Dim cell As DataGridViewCheckBoxCell = DataGridView1.Rows(e.RowIndex).Cells(0)

            DataGridViewCheckBoxColumn_Uncheck()
            cell.Value = True

        End Sub
  Private Sub DataGridViewCheckBoxColumn_Uncheck()
        For Each row As DataGridViewRow In DataGridView1.Rows
            Dim cell As DataGridViewCheckBoxCell = row.Cells(0)
            cell.Value = False
        Next
    End Sub

暂无
暂无

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

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