PDA

Click to See Complete Forum and Search --> : how to put an array of images into datagrid?


ZeBula8
Mar 28th, 2005, 09:50 PM
I have used this C# code to get all of my images url's into an array called m_arrayImageNames:


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:


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

dj4uk
Mar 29th, 2005, 10:20 AM
<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

ZeBula8
Mar 29th, 2005, 09:24 PM
this code is in the PageLoad event where m_arrImageNames is an array of string.






m_arrImageNames = Directory.GetFiles(m_strImageDirectory);
DataGrid1.DataSource = m_arrImageNames;
DataGrid1.DataBind();





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

ZeBula8
Mar 29th, 2005, 09:29 PM
okay - i'm a little closer - at least now it's putting the array names but still no images

dj4uk
Mar 30th, 2005, 02:36 AM
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

ZeBula8
Mar 31st, 2005, 08:01 PM
I found code that returns virtual from local path:


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:

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]);


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?

dj4uk
Apr 1st, 2005, 03:02 AM
I ain't quite awake this morning but try.

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:

private string[m_arrImageNames.Length] myImages;


Hopefully that'll work.

DJ