2011-12-14 120 views
2

我在VB.net中的Visual Studio 2010中創建了一個WEB報告。該報告需要顯示一張表格(請參閱附件圖片)。我想使用GridView組件,我認爲這是最合適的選擇。在數據庫中有學生的數據和標記。我無法以任何方式更改數據庫,並且必須使用Visual Studio 2010和VB。根據數據在GridView中顯示不同的圖片?

我需要做的是顯示3張不同的圖片(例如)取決於學生的分數。我在PNG中獲得了圖片文件,但這可以是靈活的。例如,超過70個將是一張微笑圖片。超過60將是正常的臉。超過40張將不帶微笑的照片。我有點難以解釋,但我希望你明白我的觀點。

所以,請指教我如何實現這一目標。我很樂意爲你提供詳細的信息。如果在客戶端和服務器端腳本之間有選擇,我更喜歡服務器端腳本。數據源可以靈活(sql或linq或其他)。

enter image description here

回答

2

您應該使用RowDataBound在你的GridView的數據綁定控件。

以下是ASPX和代碼隱藏一個完整的樣品(稱超過萬字):

<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 
+1

THanksssss XO XO多添...我可以說什麼......這是「完美」的例子......因爲我無法在任何地方找到這樣的東西,我希望它也會對其他人有用。再次感謝.. c ya周圍。 – lawphotog 2011-12-14 12:20:02