Displaying different pictures in GridView depending on the data?

前端 未结 1 1280
无人及你
无人及你 2021-01-23 05:05

I am creating a WEB report in Visual Studio 2010 in VB.net. The report needs to display a table (please see the attached image). I am thinking to use the GridView component whic

相关标签:
1条回答
  • 2021-01-23 05:21

    You should use RowDataBound to bind data to controls in your GridView.

    Following is a complete sample with aspx and codebehind(says more than thousand words):

    <style type="text/css">
        .GridViewRowStyle
        {
            background-color: #A0CFEC;
            color:Blue;
        }
        .GridViewAlternatingRowStyle
        {
            background-color:White;
            color:#15317E;
        }
        .GridViewHeaderStyle
        {
            background-color:White;
            color:#15317E;
        }
    </style>
    
    <asp:GridView ID="GridStudents" AutoGenerateColumns="false" GridLines="None" runat="server">
        <RowStyle  CssClass="GridViewRowStyle" />
        <AlternatingRowStyle CssClass="GridViewAlternatingRowStyle" />
        <HeaderStyle CssClass="GridViewHeaderStyle" />
        <Columns>
            <asp:TemplateField HeaderText="Student">
               <ItemTemplate>
                    <asp:label runat="server" ID="LblStudent" Text='<%# Bind("Student") %>'></asp:label>
                </ItemTemplate>
            </asp:TemplateField>
             <asp:TemplateField HeaderText="Mark">
               <ItemTemplate>
                    <asp:label runat="server" ID="LblMark" Text='<%# Bind("Mark") %>'></asp:label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="">
               <ItemTemplate>
                    <asp:Image runat="server" ID="ImgSmiley" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    

    Codebehind(with sample-data):

    Private Sub GridStudents_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridStudents.RowDataBound
        Select Case e.Row.RowType
            Case DataControlRowType.DataRow
                Dim row = DirectCast(e.Row.DataItem, DataRowView)
                Dim ImgSmiley = DirectCast(e.Row.FindControl("ImgSmiley"), Image)
                Select Case DirectCast(row("Mark"), Int32)
                    Case Is < 50
                        ImgSmiley.ImageUrl = "~/Images/Smiley_Grim.png"
                        ImgSmiley.ToolTip = "bad"
                    Case Is < 70
                        ImgSmiley.ImageUrl = "~/Images/Smiley_Def.png"
                        ImgSmiley.ToolTip = "ok"
                    Case Else
                        ImgSmiley.ImageUrl = "~/Images/Smiley_Laugh.png"
                        ImgSmiley.ToolTip = "fine"
                End Select
        End Select
    End Sub
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            BindData()
        End If
    End Sub
    
    Private Sub BindData()
        Dim tblStudent As New DataTable("Students")
        Dim colStudent = New DataColumn("Student", GetType(String))
        Dim colMark As New DataColumn("Mark", GetType(Int32))
        tblStudent.Columns.Add(colStudent)
        tblStudent.Columns.Add(colMark)
    
        Dim newRow = tblStudent.NewRow
        newRow("Student") = "Tom"
        newRow("Mark") = 70
        tblStudent.Rows.Add(newRow)
        newRow = tblStudent.NewRow
        newRow("Student") = "Bob"
        newRow("Mark") = 40
        tblStudent.Rows.Add(newRow)
        newRow = tblStudent.NewRow
        newRow("Student") = "Danny"
        newRow("Mark") = 60
        tblStudent.Rows.Add(newRow)
        newRow = tblStudent.NewRow
        newRow("Student") = "Sussie"
        newRow("Mark") = 40
        tblStudent.Rows.Add(newRow)
    
        GridStudents.DataSource = tblStudent
        GridStudents.DataBind()
    End Sub
    
    0 讨论(0)
提交回复
热议问题