【问题标题】:Adding an image to a DataTable将图像添加到数据表
【发布时间】:2012-07-04 16:20:27
【问题描述】:

我正在运行时创建一个 DataTable。我的目标是将图像插入 DataTable 以最终显示在 GridView 中。答案HERE 看起来很有希望,但我将它复制并粘贴到 VS 中,它根本不起作用。 “不起作用”是指当我在 GridView 中显示 DataTable 时,应该包含图片的单元格只包含字符串“System.Byte[]”

这里是 DataTable 的构造和填充的一个非常简略的版本:

DataTable dt = new DataTable();
DataColumn imagecol = new DataColumn();
dt.Columns.Add(imagecol);

DataRow newrow;

for (int x = 0, x < 10; x++)
{
    newrow = dt.NewRow();
    newrow[imagecol] = *********
    dt.Add(newrow);
}

* 部分是我一直失败的地方。

我从不将 .aspx 字段用于 GridView(ImageField、TemplateField 等),因此我正在寻找不需要编辑 .aspx 代码或“准备”具有特定数据表的 GridView 的解决方案列。让我在有人问之前回答一下。我这样做的原因是:

A) 我只学过如何从以编程方式创建的 DataTable 制作 GridView。

B) 我不喜欢这样,如果我想在某个时候更改我的字段,我必须在两个地方而不是一个地方进行编辑。

C) 应用程序的所有其他页面都使用这种样式,所以我想保持一致。

那么我尝试了什么?


DataColumn imagecol = new DataColumn();
imagecol.DataType = System.Type.GetType("System.Byte[]");
//...
newrow[amountcol] = ReadImage(Server.MapPath("~/images/dashboard/myvacstatus-ampm.png"), new string[] { ".png" });

(ReadImage 函数将图像转换为字节数组)

结果:GridView 中的字段显示文本“System.Byte[]”。


DataColumn imagecol = new DataColumn();
//...
newrow[amountcol] = ResolveUrl("~/images/dashboard/myvacstatus-ampm.png");

结果:GridView 中的字段显示文本“/images/dashboard/myvacstatus-ampm.png”


DataColumn imagecol = new DataColumn();
//...
Image timeImg = new Image();
timeImg.ImageUrl = "/images/dashboard/myvacstatus-ampm.png"
newrow[amountcol] = timeImg;

结果:GridView 中的字段显示文本“System.Web.UI.WebControls.Image”


我的最后一个要求是该字段必须具有灵活性,可以是图像或字符串。为了透视这个/看看我想要做什么,这是一个假期管理系统。假期一般为半天或全天。我们有两者的图标。但有时假期可以持续 X 小时。因此,在恰好是半天的情况下,该行需要半天的图像。如果是全天,则该行需要全天的图像。如果时间量既不是一半也不是完整的,我们需要将小时数替换为文本。

我认为可行的一种可能的解决方案是将假期的时间放在隐藏列中,然后在 RowDataBound 事件上,使用小时的隐藏值创建必要的图像或文本,然后说:

e.Row.Cells[3].Controls.Add(myImage);

我不喜欢这样,因为它再次拆分了我的代码,我必须始终只知道 Cells 索引是什么等等。我真的只想让所有这些都包含我的 BuildDataTable 函数。

【问题讨论】:

    标签: c# asp.net image gridview datatable


    【解决方案1】:

    编辑:重新阅读问题后完整地重写答案。

    你说:

    我从不将 .aspx 字段用于 GridView (ImageField, TemplateField 等),所以我正在寻找不需要的解决方案 编辑 .aspx 代码...

    如果您只想使用AutoGenerateColumns="false",那么我认为您将无法在 GridView 中显示图像。

    为了显示图像,渲染的页面必须有一个图像标签。没有办法解决这个问题,除非你开始摆弄 CSS data urls

    您的要求听起来非常复杂,而且它无论如何都不会在所有浏览器中工作。

    要显示图像,我认为您必须手动设置GridView。这是一个完整的工作示例,您可以将其用作起点(您必须调整图像 url 以匹配您的项目):

    在 aspx 文件中

    <asp:GridView ID="example" runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:BoundField HeaderText="Some other field" DataField="SomeOtherField" />
            <asp:TemplateField HeaderText="Image/Text">
                <ItemTemplate>
                    <asp:Image ID="FullDay" runat="server" Visible='<%# ((int)Eval("HoursField")) == this.FullDayHours %>'
                        ImageUrl="~/images/dashboard/fullDayImage.png" />
                    <asp:Image ID="HalfDay" runat="server" Visible='<%# ((int)Eval("HoursField")) == this.HalfDayHours %>'
                        ImageUrl="~/images/dashboard/halfDayImage.png" />
                    <asp:Literal ID="ShowHours" runat="server" Visible='<%# ((int)Eval("HoursField")) != this.HalfDayHours  && ((int)Eval("HoursField")) != this.FullDayHours %>'
                        Text='<%# Eval("HoursField") %>'></asp:Literal>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    

    代码隐藏文件

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Collections;
    using System.Data;
    
    public partial class GridViewTest : System.Web.UI.Page
    {
        public readonly int FullDayHours = 8;
        public readonly int HalfDayHours = 4;
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable gridViewSource = new DataTable();
    
                DataColumn oneColumn = new DataColumn("SomeOtherField");
                oneColumn.DataType = System.Type.GetType("System.String");
                gridViewSource.Columns.Add(oneColumn);
    
                oneColumn = new DataColumn("HoursField");
                oneColumn.DataType = System.Type.GetType("System.Int32");
                gridViewSource.Columns.Add(oneColumn);
    
                for (int i = 0; i < 10; i++)
                {
                    DataRow oneRow = gridViewSource.NewRow();
                    oneRow["SomeOtherField"] = "Other field " + i.ToString();
                    oneRow["HoursField"] = i;
                    gridViewSource.Rows.Add(oneRow);
                }
                example.DataSource = gridViewSource;
                example.DataBind();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-06-03
      • 2018-05-28
      • 1970-01-01
      • 2013-07-10
      • 1970-01-01
      • 1970-01-01
      • 2018-12-02
      • 1970-01-01
      • 2014-01-29
      相关资源
      最近更新 更多