【问题标题】:Is it possible in Gridview to show different images representing different database values?是否可以在 Gridview 中显示代表不同数据库值的不同图像?
【发布时间】:2018-03-22 17:14:13
【问题描述】:

我有一个名为 has_Amenities 的字段名。此字段名可以同时采用一个、两个、三个、四个或五个值。

值是水、电、宠物、完全连接、全部

  • 如果 has_Amenities = "electricity",则显示电力图标,但也会显示宠物、完整连接和水龙头的空白图标。

  • 如果 has_Amenities = "pets",则显示宠物图标以及电力、完整连接和水的空白图标

  • 如果 has_Amenities = "water",则显示水图标,同时显示宠物、完整连接和电力的空白图标。

  • 如果 has_Amenities = "full",则显示完整的连接图标并显示宠物、水和电的空白图标。

  • 如果 has_Amenities = "All",则表示它有电、水、完整的连接和宠物。显示所有四个图标。

我们使用的是gridview,gridview控件id是gridview1。

下面的代码只显示一个值 - 电力。

 <asp:TemplateField HeaderText="Facility Has">
   <ItemTemplate>
     <asp:Image ID="ImageDetailItem" width="22" height="22" ImageUrl='<%# IIF(CONVERT.ToString(Eval("has_Amenities")) = "Electricity", "~/images/icon_amps_50.gif", Eval("location","~/images/icon_waterfront_no.gif")) %>' runat="server" />                                        
   </ItemTemplate>
 </asp:TemplateField>

这不是正确的解决方案。

如何使用上面提到的所有 IF 条件以及如何在一个单元格中显示所有四个图标?

下面是它们的布局图。

【问题讨论】:

    标签: asp.net vb.net gridview


    【解决方案1】:

    您可以将此逻辑放入RowDataBound 事件中。当数据行(由GridViewRow 对象表示)绑定到GridView 控件中的数据时,将引发RowDataBound 事件。这使您能够提供一种事件处理方法来执行自定义例程,例如在此事件发生时修改绑定到行的数据的值。

    以下是可用于解决问题的示例代码。

    Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
    
          Dim drv As Common.DbDataRecord = CType(e.Row.DataItem, Common.DbDataRecord)
          e.Row.Cells(1).Text = "<i>" & e.Row.Cells(1).Text & "</i>"
          Image img =  e.row.Cells(7).FindControl("image1")
    
          if drv("has_Amenities") = "Electricity" Then
              img.ImageUrl = "~/images/icon_amps_50.gif"
          elseif
          ' ////////////////////////////////
          ' You can place other logic here...
    
    
        End If
    End Sub
    

    【讨论】:

      【解决方案2】:

      我在 asp.net webforms 中使用 c# 做了类似的事情(我希望 VB 没有太大的不同)

       <asp:TemplateField>
                     <ItemTemplate>
                         <asp:Image runat="server" ImageUrl="<%# ProtectedIsVisible(Item.HasAmenties,"Electricity")? "~/electricity.gif":"~/blank.gif" %>"/>
                         <asp:Image ID="Image1" runat="server" ImageUrl="<%# ProtectedIsVisible(Item.HasAmenties,"Pets")? "~/pets.gif":"~/blank.gif" %>"/>
                         <asp:Image ID="Image2" runat="server" ImageUrl="<%# ProtectedIsVisible(Item.HasAmenties,"Water")? "~/water.gif":"~/blank.gif" %>"/>
                     </ItemTemplate>
                 </asp:TemplateField>
      

      页面类有受保护的方法,包含所有逻辑

      protected bool ProtectedIsVisible(string[] values,string tag)
      

      我使用了 GridView 的 ItemType 属性,设置为强类型模型类,类似于

      class Model{ public string[] HasAmenties {get;set; }
      <asp:GridView ItemType="MyApplication.Model" runat="server" ID="MyGridView">
      

      【讨论】:

        【解决方案3】:
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    // set formatting for the category cell
                    TableCell cell = e.Row.Cells[2];
        
                    // set formatting for value cells
                    for (int i = 2; i < e.Row.Cells.Count; i++)
                    {
                        cell = e.Row.Cells[i];
        
                        int c_val = int.Parse(e.Row.Cells[i].Text);
                        if (c_val == 0)
                        {
                            //cell.BackColor = System.Drawing.Color.Red;text-align: center;
                            cell.ForeColor = System.Drawing.Color.Red;
                            cell.Attributes.Add("style", "text-align: center;");
                            cell.Text = "<i class='fa fa-remove'></i>";
                            //cell.Attributes.Add("Class", "fa fa-remove");
                        }
                        else
                        {
        
                            cell.ForeColor = System.Drawing.Color.Green;
                            cell.Attributes.Add("style", "text-align: center;");
                            cell.Text = "<i class='fa fa-check'></i>";
                            //cell.Attributes.Add("Class", "fa fa-check");
                        }
        
                    }
                }
                else if (e.Row.RowType == DataControlRowType.Header)
                {
                    for (int i = 2; i < e.Row.Cells.Count; i++)
                    { 
                        string Header_NewText = e.Row.Cells[i].Text;
                        e.Row.Cells[i].Text = Header_NewText.Replace(",", "<br/>");
                    }
                }
        
        
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-01-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-18
          • 2014-12-18
          • 2012-05-27
          • 2012-11-06
          相关资源
          最近更新 更多