How to put an image in a GridView

P

Parrot

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?
 
K

Ken Cox [Microsoft MVP]

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 said:
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?
 
P

Parrot

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 said:
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 said:
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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top