1

我在将图像放入 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 上,这将绑定到网格,图像将出现在网格视图上......

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

4

2 回答 2

1

我不知道在 GridView 中本机渲染图像文件(包括 BMP)的方法。

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

于 2013-07-03T16:55:22.423 回答
0

将二进制位图存储在 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"" />");
    }
}
于 2020-08-11T06:06:46.517 回答