【问题标题】:how to display text and image in a column of a gridview in asp.net c# [closed]如何在asp.net c#中的gridview的列中显示文本和图像[关闭]
【发布时间】:2013-02-05 16:36:02
【问题描述】:

从数据库中,如果有简单的文本,则文本应显示在网格视图中,如果表中的文本是图像文件的名称,则应显示图像

例子

id           text
----         -------
1            hello
2            hi
3            imagename.jpg

现在我想在 gridview 的 column[0] 中显示 hello 和 hi,同时在同一列中显示图像(imagename.jpg),即 column[0]... 请帮忙

【问题讨论】:

标签: c# asp.net


【解决方案1】:

您可以拥有DataGridViewImageColumn 类型的列并为图像提供文本。

DataGridViewImageColumn column = new DataGridViewImageColumn();
column.HeaderText = "Image Name";

【讨论】:

    【解决方案2】:

    我已经这样做了

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
         if (e.Row.RowType == DataControlRowType.DataRow)
         {
             string img = ((Image)e.Row.FindControl("Image1")).ImageUrl;
             string []ext=img.Split('.');
             if (ext.Length == 1)
             {
                 ((Image)e.Row.FindControl("Image1")).Visible = false;
             }
             else
             {
                 ((Image)e.Row.FindControl("Image1")).Visible = true;
             }
          }
         BindGrid();
    }
    

    【讨论】:

      【解决方案3】:
      <asp:GridView ID="grdImages" runat="server" AutoGenerateColumns="false">
          <Columns>
      
            <asp:ImageField DataImageUrlField="URL" NullDisplayText="Text when no image."/>
          </Columns>
      </asp:GridView>
      
      protected void Page_Load(object sender, EventArgs e)
      {
              grdImages.DataSource = yourDataSource;
              grdImages.DataBind();
      }
      

      【讨论】:

      • 通过此代码,我将在一列中获取名称,在另一列中获取图像,但我不希望那样......如果是文件名,则应显示图像
      • @vaishali 检查我更新的答案。没有图像 url 时使用 NullDisplayText 属性显示文本。
      猜你喜欢
      • 2015-09-17
      • 1970-01-01
      • 1970-01-01
      • 2010-10-18
      • 2010-12-21
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 2016-12-30
      相关资源
      最近更新 更多