Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 是否根据数据在GridView中显示不同的图片?_Asp.net_Vb.net_Linq_Visual Studio 2010_Gridview - Fatal编程技术网

Asp.net 是否根据数据在GridView中显示不同的图片?

Asp.net 是否根据数据在GridView中显示不同的图片?,asp.net,vb.net,linq,visual-studio-2010,gridview,Asp.net,Vb.net,Linq,Visual Studio 2010,Gridview,我正在Visual Studio 2010的VB.net中创建一个WEB报表。该报告需要显示一个表格(请参见附图)。我正在考虑使用GridView组件,我认为这是最合适的选择。数据库中有学生的数据和分数。我不能以任何方式更改数据库,我必须使用Visual Studio 2010和VB 我需要做的是根据学生的分数显示3张不同的图片(例如)。我得到的图片文件在PNGs,但这可以是灵活的。例如,超过70将是一个微笑的图片。60岁以上将是正常的脸。40岁以上的人将是一张没有笑容的照片。我有点难以解释,但

我正在Visual Studio 2010的VB.net中创建一个WEB报表。该报告需要显示一个表格(请参见附图)。我正在考虑使用GridView组件,我认为这是最合适的选择。数据库中有学生的数据和分数。我不能以任何方式更改数据库,我必须使用Visual Studio 2010和VB

我需要做的是根据学生的分数显示3张不同的图片(例如)。我得到的图片文件在PNGs,但这可以是灵活的。例如,超过70将是一个微笑的图片。60岁以上将是正常的脸。40岁以上的人将是一张没有笑容的照片。我有点难以解释,但我希望你明白我的意思

所以请告诉我如何才能做到这一点。我是个新手,请尽可能详细地说。如果在客户端脚本和服务器端脚本之间有选择,我更喜欢服务器端脚本。数据源可以是灵活的(sql、linq或任何东西)

您应该使用将数据绑定到GridView中的控件

以下是一个完整的aspx和codebehind示例(超过千字):


.GridViewRowStyle
{
背景色:#A0CFEC;
颜色:蓝色;
}
.GridViewAlternatingRowStyle
{
背景色:白色;
颜色:#15317E;
}
.GridViewHeaderStyle
{
背景色:白色;
颜色:#15317E;
}
代码隐藏(带有示例数据):

Private Sub-GridStudents\u rowdabund(发送者作为对象,e作为System.Web.UI.WebControls.GridViewRowEventArgs)处理GridStudents.rowdabund
选择Case e.Row.RowType
案例DataControlRowType.DataRow
Dim row=DirectCast(e.row.DataItem,DataRowView)
Dim ImgSmiley=DirectCast(例如,Row.FindControl(“ImgSmiley”),图像)
选择案例DirectCast(行(“标记”),Int32)
病例<50
ImgSmiley.ImageUrl=“~/Images/Smiley\u Grim.png”
ImgSmiley.ToolTip=“坏”
病例<70例
ImgSmiley.ImageUrl=“~/Images/Smiley_Def.png”
ImgSmiley.ToolTip=“确定”
其他情况
ImgSmiley.ImageUrl=“~/Images/Smiley\u Laugh.png”
ImgSmiley.ToolTip=“很好”
结束选择
结束选择
端接头
受保护的子页加载(ByVal sender作为对象,ByVal e作为System.EventArgs)处理Me.Load
如果不是的话,我会回来的
BindData()
如果结束
端接头
私有子绑定数据()
Dim TBL学生作为新数据表(“学生”)
Dim colStudent=新数据列(“学生”,GetType(字符串))
Dim colMark作为新的数据列(“Mark”,GetType(Int32))
tblStudent.Columns.Add(colStudent)
tblStudent.Columns.Add(colMark)
Dim newRow=tblStudent.newRow
纽罗(“学生”)=“汤姆”
纽罗(“马克”)=70
tblStudent.Rows.Add(newRow)
newRow=tblStudent.newRow
纽罗(“学生”)=“鲍勃”
纽罗(“马克”)=40
tblStudent.Rows.Add(newRow)
newRow=tblStudent.newRow
纽罗(“学生”)=“丹尼”
纽罗(“马克”)=60
tblStudent.Rows.Add(newRow)
newRow=tblStudent.newRow
纽罗(“学生”)=“苏茜”
纽罗(“马克”)=40
tblStudent.Rows.Add(newRow)
GridStudents.DataSource=tblStudent
GridStudents.DataBind()
端接头

非常感谢您的xo xo。。。我能说什么。。这是一个“完美”的例子。。。。。。由于我在任何地方都找不到这样的东西,我希望它也能对其他人有用。。再次感谢。。让你四处看看。
<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>
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