VB.net webform将数据从Sql数据库提取到gridview。
我有两个位列Rush和Normal - 在后面的代码中,如果Rush被检查,则行单元格变为红色,而正常变为蓝色。
我遇到的问题是该位是真还是假没有太多运气将它们转换为Integer或Int32。
这是我正在使用的代码,如果单元格7不等于1,此代码将所有行变为蓝色。
如果我去Rush cell(10)错误输入字符串的格式不正确。
问题是如何将位从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
答案 0 :(得分:0)
为什么要转换为整数,当您有位列时,请尝试使用以下代码。
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
答案 1 :(得分:0)
我想我在这里,而不是与布尔战斗,我决定 将单元格更改为整数。请检查并让我知道你的想法。 它有效,拉什订单是红色,正常是蓝色..
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