Victor Franchi
Victor Franchi

Reputation: 163

Putting image in DataTable and setting table as datasource to gridview

I'm having problems with putting an image in a DataTable and setting this table as datasource to gridview, I've searched everywhere, but no I had no luck. Here is the problem:

    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;
    }

I've been trying everything I can, creating a new column like var newColumn= new DataColumn(row[6].ToString(),typeof(Bitmap)); / converting to an image and putting there / changing the datatype of the column before adding to the DataTable... But no luck... I just need the right way to get an image from Properties.Resources and putting it on a DataTable, that will bind to a grid and the image will appear on the gridview...

Any help is precious to me right now =D

Upvotes: 1

Views: 2238

Answers (2)

Fidel Orozco
Fidel Orozco

Reputation: 1046

Storing the binary Bitmap in the DataTable only works in Windows Forms with the DataGridView Control. In ASP.NET DataGrid you must reference to a image URL. The simple way is enable in your DataGrid the Event RowDataBound.

Then wire up with code to your image location.

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"" />");
    }
}

Upvotes: 0

Garrison Neely
Garrison Neely

Reputation: 3289

I'm not aware of a method for natively rendering image files (including BMP) in a GridView.

In your case, what I'd do is save that bitmap in a local file in the virtual directory. Once you have the file name, you can bind that filename to an ImageField column in the GridView.

Upvotes: 1

Related Questions