【问题标题】:VB.net Webform Gridview with checkbox DataControlRowType带有复选框 DataControlRowType 的 VB.net Webform Gridview
【发布时间】:2017-03-29 05:13:16
【问题描述】:

VB.net webform 将数据从 Sql 数据库拉取到 gridview。

我有两个位列 Rush 和 Normal - 在后面的代码中,如果 Rush 被选中,则行单元格变为红色,Normal 变为蓝色。

我遇到的问题是位是 True 或 False 没有太多运气将它们转换为 Integer 或 Int32。

这是我正在使用的代码,如果单元格 7 不等于 1,此代码会将所有行变为蓝色。

如果我转到 Rush cell(10) 错误输入字符串格式不正确。

问题是如何将位从真/假转换为 1/0 或正确格式。

Protected Sub OnRowDataBound(sender As Object, e As GridViewRowEventArgs)

    If e.Row.RowType = DataControlRowType.DataRow Then

        Dim sh As String = Convert.ToInt32(e.Row.Cells(7).Text)

        For Each cell As TableCell In e.Row.Cells
            If sh = 1 Then
                cell.BackColor = Drawing.Color.Red
            Else
                cell.BackColor = Drawing.Color.Blue

            End If

        Next


    End If



End Sub

【问题讨论】:

  • 你能把你的aspx页面的代码贴在这里
  • 状态表单或插入表单,以及如何在此页面上添加更多代码

标签: asp.net vb.net


【解决方案1】:

为什么要转换为整数,当你有一个位列时,试试下面的代码。

Protected Sub OnRowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
    Dim sh As Boolean = Convert.ToBoolean(e.Row.Cells(7).Text)
    For Each cell As TableCell In e.Row.Cells
        If sh Then
            cell.BackColor = Drawing.Color.Red
        Else
            cell.BackColor = Drawing.Color.Blue
        End If
    Next
End If
End Sub

【讨论】:

  • 抛出错误字符串不被识别为有效的布尔值,这是我一直遇到的
【解决方案2】:

我想我有它,而不是与布尔战斗,我决定 将单元格更改为整数。请检查并让我知道您的想法。 它有效,紧急订单是红色的,正常是蓝色的..

Protected Sub OnRowDataBound(sender As Object, e As GridViewRowEventArgs)


    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim Chkb As Integer = (e.Row.DataItem(10))

        For Each cell As TableCell In e.Row.Cells
            If Chkb = -1 Then
                cell.BackColor = Drawing.Color.Red
            Else
                cell.BackColor = Drawing.Color.Blue

            End If

        Next


    End If



End Sub

【讨论】:

    猜你喜欢
    • 2011-08-04
    • 1970-01-01
    • 2017-02-18
    • 1970-01-01
    • 1970-01-01
    • 2018-11-23
    • 2018-06-21
    • 1970-01-01
    • 2011-12-11
    相关资源
    最近更新 更多