C# 将图像放入DataTable并将表设置为gridview的数据源

C# 将图像放入DataTable并将表设置为gridview的数据源,c#,asp.net,gridview,datatable,C#,Asp.net,Gridview,Datatable,我在将图像放入数据表并将此表设置为gridview的数据源时遇到问题,我到处都搜索过,但没有,我没有运气。 问题是: private void populateGrid() { // Gets a datatable from a stored procedure DataTable ragTable = ProjetoBO.HistoricoRAGStatusProjeto(Convert.ToInt32(Session["idProjeto"])

我在将图像放入数据表并将此表设置为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;
    }
private void populateGrid()
{
//从存储过程获取数据表
DataTable ragTable=ProjetoBO.HistoricoRAGStatusProjeto(Convert.ToInt32(Session[“idProjeto”]);
int组=-1;
int cont=1;
var table=GetDataTable(ragTable);
DataRow DataRow=table.NewRow();
foreach(ragTable.Rows中的数据行)
{
cont++;
if(转换为32(第[9]行)!=组)
{
cont=1;
group=Convert.ToInt32(第[9]行);
table.Rows.Add(dataRow);
dataRow=tabelaFinal.NewRow();
}
dataRow[0]=DateTime.Parse(行[2].ToString()).ToSortDateString();
//放置一些随机图像只是为了测试
dataRow[cont]=Properties.Resources.myIcon;
}
//我的网格
histRagStatus.DataSource=表格;
histRagStatus.DataBind();
}
//创建包含所需列的数据表
私有数据表GetDataTable(数据表ragTable)
{
var newTable=新数据表();
新增表格。列。添加(“日期”);
foreach(ragTable.Rows中的数据行)
{
如果(!newTable.Columns.Cast().Any(column=>column.ColumnName.Equals(行[6].ToString()))
{
var newColumn=newdatacolumn(行[6].ToString());
newTable.Columns.Add(newColumn);
}
}
返回newTable;
}
我一直在尽我所能,创建一个新列,如var newColumn=newdatacolumn(行[6].ToString(),typeof(位图));/正在转换为图像并在添加到数据表之前放置/更改列的数据类型。。。但是没有运气。。。我只需要用正确的方法从Properties.Resources获取一个图像,并将其放在一个数据表上,该数据表将绑定到一个网格,该图像将显示在gridview上


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

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


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

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


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

在DataTable中存储二进制位图仅适用于带有DataGridView控件的Windows窗体。在ASP.NET DataGrid中,必须引用图像URL。简单的方法是在DataGrid中启用事件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"" />");
    }
}
受保护的无效GridView1\u行数据绑定(对象发送方,GridViewRowWEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
//在图像列索引中设置。通过示例设置0
e、 行。单元格[0]。Text=Server.HtmlDecode(@“”);
}
}

在DataTable中存储二进制位图仅适用于带有DataGridView控件的Windows窗体。在ASP.NET DataGrid中,必须引用图像URL。简单的方法是在DataGrid中启用事件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"" />");
    }
}
受保护的无效GridView1\u行数据绑定(对象发送方,GridViewRowWEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
//在图像列索引中设置。通过示例设置0
e、 行。单元格[0]。Text=Server.HtmlDecode(@“”);
}
}

图像数据是如何发送的?字节数组?它是以位图的形式发送的图像数据是如何发送的?字节数组?它作为位图发送,但我的所有列都是动态创建的,所以我的gridview没有用于放置图像字段的columns标记,它是空的,如下所示。我将所有内容添加到DataTable,然后将其绑定到网格。。。有没有办法在数据表中添加图像字段?您是否有Windows开发背景?这是WinForms和WebForms不同的情况之一。您只能将GridView绑定到图像url,而不能绑定到图像本身。我也尝试过将图像url(这是计算机上某个目录的本地url)放到datatable行中,但效果相同,它会将字符串url而不是图像。。。我做错了什么?因为它是一个网站,URL必须位于虚拟目录(网站的目录结构)中的某个位置。通常它会以一个波浪形开头(比如~/images/image1.bmp)。无论如何,您需要在GridView中为图像设置一列,或者必须在绑定的每一行中动态添加一个asp:Image。是的,我使用了图像的虚拟目录“~/Content/img.jpg”,但它也得到了字符串。但问题是:我不在网格中创建列,我只是将datatable绑定到空网格。我可以在dataTable上添加templateItem或Imagefield吗?我也尝试过类似的方法,但可以让它工作,但我的所有列都是动态创建的,所以我的gridview没有用于放置图像字段的columns标记,它是空的,就像这样。我将所有内容添加到DataTable,然后将其绑定到网格。。。有没有办法在数据表中添加图像字段?您是否有Windows开发背景?这是WinForms和WebForms不同的情况之一。您只能将GridView绑定到图像url,而不能绑定到图像本身。我也尝试过将图像url(这是指向我计算机上某个目录的本地url)放到datatable行中,但它也会产生相同的效果