Mar 28th, 2005, 10:50 PM
#1
Thread Starter
Fanatic Member
how to put an array of images into datagrid?
I have used this C# code to get all of my images url's into an array called m_arrayImageNames:
Code:
m_arrImageNames = Directory.GetFiles(m_strImageDirectory);
I put a datagrid onto my webform but i cannot figure out how to get the images into the grid.
I did a search and could only find information on how to load images from a database - not what I am trying to do.
Here is the .aspx code which doesnt' work:
Code:
<Columns>
<asp:TemplateColumn>
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<HeaderTemplate>
Images
</HeaderTemplate>
<ItemTemplate>
<asp:Image id="Image2" runat="server"></asp:Image>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
Can someone help?
Mar 29th, 2005, 11:20 AM
#2
Frenzied Member
Re: how to put an array of images into datagrid?
Code:
<Columns>
<asp:TemplateColumn>
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<HeaderTemplate>
Images
</HeaderTemplate>
<ItemTemplate>
<asp:Image id="Image2" runat="server" ImageUrl="<# Container.DataItem %>"/>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
I'm presuming you have set the array as a datasource and databound it to the datagrid?
DJ
Mar 29th, 2005, 10:24 PM
#3
Thread Starter
Fanatic Member
Re: how to put an array of images into datagrid?
this code is in the PageLoad event where m_arrImageNames is an array of string.
Code:
m_arrImageNames = Directory.GetFiles(m_strImageDirectory);
DataGrid1.DataSource = m_arrImageNames;
DataGrid1.DataBind();
Code:
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 105; LEFT: 184px; POSITION: absolute; TOP: 16px"
runat="server" Width="239px" AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn HeaderText="Crystals">
<ItemTemplate>
<asp:Image id=Image2 runat="server" ImageAlign="Left" ImageUrl="<% Container.DataItem %>">
</asp:Image>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid></form>
all in all it still doesn't pick up the images - take a look at the screenshot -
why does ms have to make this so difficult to do?
Attached Images
Mar 29th, 2005, 10:29 PM
#4
Thread Starter
Fanatic Member
Re: how to put an array of images into datagrid?
okay - i'm a little closer - at least now it's putting the array names but still no images
Attached Images
Mar 30th, 2005, 03:36 AM
#5
Frenzied Member
Re: how to put an array of images into datagrid?
OK so it is returning the local path to the file not the virtual path as used on the website.
i.e. although the image is stored in C:\inetpub\wwwroot\Rosary\images\traditional\citrine.jpg it needs to displayed as http://localhost/Rosary/images/citrine.jpg (obviously this could be slightly different depending on your setup).
I know there is a method to get the local path from a virtual path but not the other way around. Try using the replace method on the string to give the correct path.
HTH
DJ
If I have been helpful please rate my post. If I haven't tell me!
Mar 31st, 2005, 09:01 PM
#6
Thread Starter
Fanatic Member
Re: how to put an array of images into datagrid?
I found code that returns virtual from local path:
Code:
private string GetImageURL(string strImageFilePath)
{
// Request.Url.GetLeftPart(UriPartial.Authority) yields
// "http://{domain name}"
// Request.ApplicationPath yields "/{virtual directory}"
string imagePath;
imagePath = Request.Url.GetLeftPart(UriPartial.Authority) +
@"/Rosary" + Request.ApplicationPath + @"/traditional/" +
strImageFilePath.Substring(m_strImageDirectory.Length + 1);
return imagePath;
}
with this function I can pass in the string of the path that's in the array like this:
Code:
m_arrImageNames = Directory.GetFiles(m_strImageDirectory);
for(int i = 1;i < m_arrImageNames.Length;++i)
{
myImages[i] = GetImageURL(m_arrImageNames[i]);
}
DataGrid2.DataSource = myImages;
DataGrid2.DataBind();
But then I get this error on line 52:
myImages[i] = GetImageURL(m_arrImageNames[i]);
Code:
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 50: for(int i = 1;i < m_arrImageNames.Length;++i)
Line 51: {
Line 52: myImages[i] = GetImageURL(m_arrImageNames[i]);
Line 53:
Line 54: }
I have myImages defined like so:
private string[] myImages;
I tried changing string[] to object[] but it didn't like that either -
what am i doing wrong?
Apr 1st, 2005, 04:02 AM
#7
Frenzied Member
Re: how to put an array of images into datagrid?
I ain't quite awake this morning but try.
Code:
for(int i = 0;i < m_arrImageNames.Length;i++)
{
myImages[i] = GetImageURL(m_arrImageNames[i]);
}
Also try defining the array just before the loop like so:
Code:
private string[m_arrImageNames.Length] myImages;
Hopefully that'll work.
DJ
If I have been helpful please rate my post. If I haven't tell me!
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width