【问题标题】:Reading the value of the cell读取单元格的值
【发布时间】:2011-05-15 18:47:48
【问题描述】:

我有一些用 vb.net 编写的DataGridView 代码。 (datasource 没有附加任何内容。)

第 4 列是 checkboxCell。如何检测 checkBox 是选中还是未选中?

此代码奇怪地随机报告 TRUE 或 FALSE。它甚至会打开 checkbox 在我单击的行之外的行中。

Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
   Dim whichGrid As DataGridView = CType(sender, DataGridView)

   Dim rowClicked As Int16 = e.RowIndex

   Call MsgBox(rowClicked & vbCrLf & whichGrid.Rows(rowClicked).Cells(4).Value)

End Sub

我在这里(和其他地方)看到的所有其他示例似乎都没有帮助。他们的解决方案总是:

  • 只需检查单元格的 VALUE。
  • 只要学习 c#,然后学习将其转换为 vb.net。
  • 只需检查 VALUE 是否为空、null、“”或所有这些。
  • 将 VALUE 转换为布尔值。
  • 改为将其附加到数据源。
  • 设置 TrueValue 和 FalseValue。

我尝试了无数其他方法,似乎没有一个真正得到 vb.net 中的checkbox ON/OFF 值。

【问题讨论】:

    标签: vb.net winforms datagridview


    【解决方案1】:

    将单元格的值转换为布尔值:

    Dim RowIndex As Integer = ...
    Dim ColumnIndex As Integer = ...
    
    Dim IsTicked As Boolean = CBool(DataGridView1.Rows(RowIndex).Cells(ColumnIndex).Value)
    
    If IsTicked Then
        MessageBox.Show("You ticked the box.")
    Else
        MessageBox.Show("You cleared the box.")
    End If
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多