简体   繁体   English

VB.net Webform Gridview与复选框DataControlRowType

[英]VB.net Webform Gridview with checkbox DataControlRowType

VB.net webform pulling data from Sql database to gridview. VB.net Webform将数据从Sql数据库提取到gridview。

I have two Bit Columns Rush and Normal - In the code behind if Rush is Checked then the row cells turn Red and Normal turns blue. 我有两个位列Rush和Normal-在后面的代码中,如果选中Rush,则行单元格变为红色,Normal变为蓝色。

The problem I have is the bit is True or False not having much luck converting them to Integer or Int32. 我的问题是,位是True还是False,将它们转换为Integer或Int32的运气不好。

Here is a code I am working with, this code turn all rows blue , if cell 7 not equal to 1. 这是我正在使用的代码,如果单元格7不等于1,此代码会将所有行变为蓝色。

If I go to Rush cell(10) error input string not in correct format. 如果我转到Rush cell(10)错误,则输入的字符串格式不正确。

Question is how to convert bit from True/false to 1/0 or correct format. 问题是如何将位从True / false转换为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

Why converting to an integer, when you are having a bit column, try with the below code. 为什么要转换为整数,当您有位列时,请尝试以下代码。

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

I think I have it here , instead of fighting the Boolean , I decided to change the cells to Integer. 我想在这里有它,而不是与布尔值作斗争,我决定将单元格更改为Integer。 Please check over and let me know your thoughts. 请检查并让我知道您的想法。 It works, Rush orders are red and normal are blue .. 它正常工作,紧急订单为红色,正常为蓝色..

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

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

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