views:

17

answers:

1

I have a datagridview and bind a table with blob field to it which contains simple text and not image. After this datagridview tries to show the text as image and generates an exception. Is it possible to disable datagridview from displaying images in correspondence to blob fields?

The code is approximately like this:

// maybe datagridview has some property to set in order to disable such behavior?
gridView.DataSource = mytable_with_blob_field;
A: 

You can set AutoGenerateColumns to false and add the non-blob fields manually.

<asp:BoundField HeaderText="Header text" DataField="FieldToBind" />

For the blob columns add a TemplateField instead.

<asp:TemplateField HeaderText="Blob as Text">
    <ItemTemplate>
        <asp:Literal ID="blob" runat="server" Text='<%# new UTF8Encoding().GetString((Container.DataItem as Custom).Blob) %>' />
    </ItemTemplate>
</asp:TemplateField>

Resulting into something like this:

<asp:GridView ID="grid" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField HeaderText="Header text" DataField="FieldToBind" />
        <asp:TemplateField HeaderText="Blob as Text">
            <ItemTemplate>
                <asp:Literal ID="blob" runat="server" Text='<%# new UTF8Encoding().GetString((Container.DataItem as Custom).Blob) %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Here is the full test code from the server-side:

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;

namespace TestWeb
{
    public class Custom
    {
        public string FieldToBind { get; set; } // other fields
        public byte[] Blob { get; set; } // your blob
    }

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var list = new List<Custom>();
            list.Add(new Custom { FieldToBind = "pure text", Blob = new UTF8Encoding().GetBytes("blob") });
            grid.DataSource = list;
            /*grid.RowDataBound += (rS, rE) =>
            {
                if (rE.Row.RowType == DataControlRowType.DataRow)
                {
                    // could also be bound by the server-side
                    //(rE.Row.FindControl("blob") as ITextControl).Text = new UTF8Encoding().GetString((rE.Row.DataItem as Custom).Blob);
                }
            };*/
            grid.DataBind();
        }
    }
}
BrunoLM