【问题标题】:Putting image in DataTable and setting table as datasource to gridview将图像放入 DataTable 并将表设置为 gridview 的数据源
【发布时间】:2013-07-01 10:55:00
【问题描述】:

我在将图像放入 DataTable 并将该表设置为 gridview 的数据源时遇到问题,我到处搜索,但没有运气。 问题来了:

    private void populateGrid()
    {
       // Gets a datatable from a stored procedure
        DataTable ragTable = ProjetoBO.HistoricoRAGStatusProjeto(Convert.ToInt32(Session["idProjeto"]));

        int group= -1;

        int cont = 1;

        var table = GetDataTable(ragTable);

        DataRow dataRow = table.NewRow();

        foreach (DataRow row in ragTable.Rows)
        {
            cont++;
            if (Convert.ToInt32(row[9]) != group)
            {
                cont = 1;
                group= Convert.ToInt32(row[9]);
                table.Rows.Add(dataRow);
                dataRow = tabelaFinal.NewRow();
            }
            dataRow[0] = DateTime.Parse(row[2].ToString()).ToShortDateString();

            //putting some random image just for testing purpose
            dataRow[cont] = Properties.Resources.myIcon;

        }
        //my grid
        histRagStatus.DataSource = table ;
        histRagStatus.DataBind();
    }
    //Creates a dataTable with the columns I need
    private DataTable GetDataTable(DataTable ragTable)
    {
        var newTable= new DataTable();

        newTable.Columns.Add("Date");

        foreach (DataRow row in ragTable.Rows)
        {
            if (!newTable.Columns.Cast<DataColumn>().Any(column => column.ColumnName.Equals(row[6].ToString())))
            {
                var newColumn= new DataColumn(row[6].ToString());
                newTable.Columns.Add(newColumn);
            }
        }
        return newTable;
    }

我一直在尽我所能,创建一个新列,如 var newColumn= new DataColumn(row[6].ToString(),typeof(Bitmap)); / 转换为图像并放在那里 / 在添加到 DataTable 之前更改列的数据类型...但是没有运气...我只需要正确的方法从 Properties.Resources 获取图像并将其放在 DataTable 上,这将绑定到网格,图像将出现在 gridview...

现在任何帮助对我来说都很宝贵=D

【问题讨论】:

  • 图片数据是如何发送的?一个字节数组?
  • 它作为位图发送

标签: c# asp.net gridview datatable


【解决方案1】:

在 DataTable 中存储二进制位图仅适用于带有 DataGridView 控件的 Windows 窗体。在 ASP.NET DataGrid 中,您必须引用图像 URL。简单的方法是在您的 DataGrid 中启用 Event RowDataBound。

然后将代码连接到您的图像位置。

protected void GridView1_RowDataBound(object sender, GridViewRowWEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        //Setup in your image column index. By Example setting 0
        e.Row.Cells[0].Text=Server.HtmlDecode(@"<img src=""./Images/pdf.gif"" />");
    }
}

【讨论】:

    【解决方案2】:

    我不知道在 GridView 中本地呈现图像文件(包括 BMP)的方法。

    在您的情况下,我要做的是将该位图保存在虚拟目录中的本地文件中。获得文件名后,您可以将该文件名绑定到 GridView 中的 ImageField 列。

    【讨论】:

    • 但是我所有的列都是动态创建的,所以我的gridview没有列标签来放置图像字段,它是空的,就像这样 。我将所有内容添加到 DataTable,然后将其绑定到网格...有没有办法在数据表中添加图像字段?
    • 您有 Windows 开发背景吗?这是 WinForms 和 WebForms 不同的情况之一。您只能将 GridView 绑定到图像 URL,而不是图像本身。
    • 好吧,我也尝试将图像 url(这是我计算机上目录的本地 url)放在数据表行中,但效果相同,它放置了字符串 url 而没有图片...我做错了什么?
    • 因为它是一个网站,所以 URL 必须位于虚拟目录(网站的目录结构)中的某个位置。通常它会以波浪号开头(如 ~/images/image1.bmp)。无论如何,您需要在 GridView 中为图像设置一列,或者您必须为绑定的每一行动态添加一个 asp:Image。
    • 是的,我使用了图像“~/Content/img.jpg”的虚拟目录,但它也得到了字符串。但事情是这样的:我不在网格中创建列,我只是将数据表绑定到空网格。我可以在 dataTable 上添加 templateItem 或 Imagefield 吗?我尝试过类似的方法,但可以设法使其工作
    猜你喜欢
    • 2013-05-04
    • 1970-01-01
    • 2011-08-27
    • 2012-09-16
    • 2016-03-31
    • 1970-01-01
    • 2014-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多