How to put an image in a GridView

Discussion in 'ASP .Net Web Controls' started by Parrot, Jul 8, 2006.

  1. Parrot

    Parrot Guest

    I cannot get an image to display in a column of my GridView control. I build
    the fields at run time and create a table containing my data and then bind it
    to the Gridview. My cod e is shown below:

    DataTable dt = dataSet1.Tables.Add("Imagetable");
    dt.Columns.Add(new DataColumn("File path", typeof(string)));
    dt.Columns.Add(new DataColumn("Image file",
    typeof(System.Web.UI.WebControls.Image)));

    // this is a loop
    {

    DataRow dr = dataSet1.Tables["Imagetable"].NewRow();

    dr[0] = name.ToString();
    System.Web.UI.WebControls.Image image = new
    System.Web.UI.WebControls.Image();
    image.ImageUrl = dirpath + name;
    image.Height = 50;
    image.Visible = true;
    dr[1] = image;
    dataSet1.Tables["Imagetable"].Rows.A dd(dr);
    }

    DataView v = new DataView(dt);
    GridView1.DataSource = v;
    GridView1.DataBind();

    My first column displays ok but the second column which is the image column
    does not even display a column at all or even the heading. Does anyone know
    what the problem is?
     
    Parrot, Jul 8, 2006
    #1
    1. Advertising

  2. > dt.Columns.Add(new DataColumn("Image file",
    > typeof(System.Web.UI.WebControls.Image)));


    Is System.Web.UI.WebControls.Image a legal datatype value? Shouldn't it be
    Byte?

    http://msdn.microsoft.com/library/d...lrfsystemdatadatacolumnclassdatatypetopic.asp

    Ken
    Microsoft MVP [ASP.NET]

    "Parrot" <> wrote in message
    news:...
    >I cannot get an image to display in a column of my GridView control. I
    >build
    > the fields at run time and create a table containing my data and then bind
    > it
    > to the Gridview. My cod e is shown below:
    >
    > DataTable dt = dataSet1.Tables.Add("Imagetable");
    > dt.Columns.Add(new DataColumn("File path", typeof(string)));
    > dt.Columns.Add(new DataColumn("Image file",
    > typeof(System.Web.UI.WebControls.Image)));
    >
    > // this is a loop
    > {
    >
    > DataRow dr = dataSet1.Tables["Imagetable"].NewRow();
    >
    > dr[0] = name.ToString();
    > System.Web.UI.WebControls.Image image = new
    > System.Web.UI.WebControls.Image();
    > image.ImageUrl = dirpath + name;
    > image.Height = 50;
    > image.Visible = true;
    > dr[1] = image;
    > dataSet1.Tables["Imagetable"].Rows.A dd(dr);
    > }
    >
    > DataView v = new DataView(dt);
    > GridView1.DataSource = v;
    > GridView1.DataBind();
    >
    > My first column displays ok but the second column which is the image
    > column
    > does not even display a column at all or even the heading. Does anyone
    > know
    > what the problem is?
     
    Ken Cox [Microsoft MVP], Jul 9, 2006
    #2
    1. Advertising

  3. Parrot

    Parrot Guest

    I tried changing the datatype to byte but that resulted in a data exception.
    In a nutshell I cannot get an image file to display at all if I define it
    dynamically in a table. I tried the approach suggested by another writer in
    this forum in which I created an Image file when defining the Gridview using
    the Smart tag editing feature. Then I assigned to the DataImageUrl Field the
    name of a string field which I dynamically created at run time in the same
    way that I created the image field that wouldn't work. By using this
    approach I assigned to this string field the path of the image file that I
    wish to display. This approach worked for some of the image files but not
    all of them. I could not find a pattern as to why some image files would
    display and some not - they all went through a loop of the file directory. I
    am at a loss as to what to do next. What should be a simple procedure is
    turning into another time burning process. I can tell you that Visual Studio
    2005 has been a very time consuming and frustrating tool to work with,
    especially in converting existing applications from VS 2003. In retrospect I
    wished that I had left everything under 2003. The fact that I have to upload
    cs files as well as aspx files to the server was not at all intuitive to me
    and seems to a step backwards. VS 2005 is not ready for prime time.



    "Ken Cox [Microsoft MVP]" wrote:

    > > dt.Columns.Add(new DataColumn("Image file",
    > > typeof(System.Web.UI.WebControls.Image)));

    >
    > Is System.Web.UI.WebControls.Image a legal datatype value? Shouldn't it be
    > Byte?
    >
    > http://msdn.microsoft.com/library/d...lrfsystemdatadatacolumnclassdatatypetopic.asp
    >
    > Ken
    > Microsoft MVP [ASP.NET]
    >
    > "Parrot" <> wrote in message
    > news:...
    > >I cannot get an image to display in a column of my GridView control. I
    > >build
    > > the fields at run time and create a table containing my data and then bind
    > > it
    > > to the Gridview. My cod e is shown below:
    > >
    > > DataTable dt = dataSet1.Tables.Add("Imagetable");
    > > dt.Columns.Add(new DataColumn("File path", typeof(string)));
    > > dt.Columns.Add(new DataColumn("Image file",
    > > typeof(System.Web.UI.WebControls.Image)));
    > >
    > > // this is a loop
    > > {
    > >
    > > DataRow dr = dataSet1.Tables["Imagetable"].NewRow();
    > >
    > > dr[0] = name.ToString();
    > > System.Web.UI.WebControls.Image image = new
    > > System.Web.UI.WebControls.Image();
    > > image.ImageUrl = dirpath + name;
    > > image.Height = 50;
    > > image.Visible = true;
    > > dr[1] = image;
    > > dataSet1.Tables["Imagetable"].Rows.A dd(dr);
    > > }
    > >
    > > DataView v = new DataView(dt);
    > > GridView1.DataSource = v;
    > > GridView1.DataBind();
    > >
    > > My first column displays ok but the second column which is the image
    > > column
    > > does not even display a column at all or even the heading. Does anyone
    > > know
    > > what the problem is?

    >
    >
    >
     
    Parrot, Jul 9, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?bWdvbnphbGVzMw==?=

    GridView Hierarchical View - Gridview in Gridview

    =?Utf-8?B?bWdvbnphbGVzMw==?=, May 9, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    18,044
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
    May 9, 2006
  2. Remy
    Replies:
    2
    Views:
    8,602
  3. Andrew Han

    put gridview into reportviewer

    Andrew Han, Mar 5, 2008, in forum: ASP .Net
    Replies:
    0
    Views:
    733
    Andrew Han
    Mar 5, 2008
  4. Gabriel Rossetti
    Replies:
    3
    Views:
    601
    Jerry Hill
    Apr 25, 2008
  5. Replies:
    4
    Views:
    4,449
Loading...

Share This Page