0

如何:添加背景颜色为蓝色的数据行和“选中”复选框,但其余部分的背景颜色为蓝色

请参阅下面的代码。谢谢您的帮助

Dim table2 As New DataTable
' Create four typed columns in the DataTable.
table2.Columns.Add("ID", GetType(Integer))
table2.Columns.Add("Drug", GetType(String))
table2.Columns.Add("Patient", GetType(String))
table2.Columns.Add("Date", GetType(DateTime))
' Add five rows with those columns filled in the DataTable.
table2.Rows.Add(25, "Indocin", "David", DateTime.Now)
table2.Rows.Add(50, "Enebrel", "Sam", DateTime.Now)
table2.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now)
table2.Rows.Add(21, "Combivent", "Janet", DateTime.Now)
table2.Rows.Add(10, "Dilantin", "Melanie", DateTime.Now)
table2.Rows.Add(125, "Indocin", "David", DateTime.Now)
table2.Rows.Add(150, "Enebrel", "Sam", DateTime.Now)
table2.Rows.Add(2, "Hydralazine", "Christoff", DateTime.Now)

'GridView1.DataSource = table2



For Each dr As DataRow In table2.Rows

    If dr("ID") = 21 Or dr("ID") = 150 Then

        ' add  a data row with a back ground colour - blue and check check box

    Else

        ' add  a data row with a back ground colour - yellow

    End If




Next

GridView1.DataBind()
4

2 回答 2

2

我会将 DataTable 绑定到 GridView 并通过RowDataBound Event进行处理。

Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowDataBound

    'Assuming your ID column is in the first cell.
    If e.Row.RowType = DataControlRowType.DataRow AndAlso _
       (e.Row.Cells(0).Text = "21" OrElse e.Row.Cells(0).Text = "150") Then

        e.Row.BackColor = Drawing.Color.Blue

        'Assuming CheckBox is in the second cell with ID of "IdOfCheckBox"
        CType(e.Row.Cells(1).FindControl("IdOfCheckBox"), CheckBox).Checked = True

    End If

End Sub
于 2012-09-06T21:13:11.900 回答
1
Sub GridView_RowDataBound(ByVal sender As Object, _
  ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)

    If e.Row.RowType = DataControlRowType.DataRow Then
                If (Check your condition ) Then
                e.Row.BackColor = Drawing.Color.Blue
                'Then find your checkbox control from row and set it's value to Checked...

   ELSE
     e.Row.BackColor = Drawing.Color.yellow

 End If
    End If
End Sub
于 2012-09-06T21:10:35.257 回答