0
votes

VB.net webform pulling data from Sql database to 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.

The problem I have is the bit is True or False not having much luck converting them to Integer or Int32.

Here is a code I am working with, this code turn all rows blue , if cell 7 not equal to 1.

If I go to Rush cell(10) error input string not in correct format.

Question is how to convert bit from True/false to 1/0 or correct format.

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
2
can you please post code of your aspx page over here - Kevin Shah
Status form , or Insert form , also how to add more code on this page - Lookup12

2 Answers

0
votes

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
0
votes

I think I have it here , instead of fighting the Boolean , I decided to change the cells to 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