datagrid problem

  • Thread starter Thread starter Morten
  • Start date Start date
M

Morten

Hi!

I have a problem displaying some values in a datagrid. I have an array that
consists of a number of objects. Each object has 2 properties: Name and a
list of web addresses. (e.g: Name: "Test", URLs: {"address1",
"adress2"...}). How can I implement this using Visual Studio?

I'd also like to be able to edit the properties so I'm hoping to be able to
use the standard routines that are available in the datagrid.

Help is appreciated.

Morten
 
Hi,

It/s very easy using databinding:

<asp:templatecolumn ItemStyle-VerticalAlign="Top" ItemStyle-Width="90"
ItemStyle-HorizontalAlign="left" >
<itemtemplate>
<span ><%#
((CtpRecord)Container.DataItem).DateRequested.ToShortDateString()%></span>
</itemtemplate>
</asp:templatecolumn>

There I use an array of CtpRecord objects , this class has a property
DateRequested that I convert to the required format.

To edit it you define the <EditItemTemplate> and include the controls that
you need, usually a TextBox and set the value using the same way as above

Cheers,
 
Hi!

Thanks for your reply. I've put the following in my webform:

<asp:templatecolumn ItemStyle-VerticalAlign="Top" ItemStyle-Width="90"
ItemStyle-HorizontalAlign="left" >
<itemtemplate>
<span ><%#((SiteClassobj)Container.DataItem).Headers.ToString()%></span>
</itemtemplate>

When I run the application I get the following error:

The type or namespace name 'SiteClassobj' could not be found (are you
missing a using directive or an assembly reference?)

The object I'm using is defined in a web service I'm calling. I can
reference the class in my code behind file but I'm not sure how to access it
in the web form.

I'm also not sure about the "ToString()" method because the return value is
an array of strings...

Morten
 
Hi,


Sorry for forget to mention it,
You have to add your namespace to the page:
<% @Import Namespace="CTPCore" %>

Cheers,
 
Hi,

Did you try to import the namespace where your class is defined?

DataBinder.Eval use reflection to get the value of the property, I think a
cast is faster.

but they do work the same :)

Cheers,
 
Hi!

It turns out that it didn't work after all. I confused myself a little
bit...

I tried the following and imported the name space that is used in my code
behind file:

<% @Import Namespace="IISServiceCall.iisservice" %>

<Columns>
<asp:TemplateColumn>
<itemtemplate>
<%#((SiteClass)Container.DataItem).Hostheaders%>
</itemtemplate>
</asp:TemplateColumn>
</Columns>

This returns the following value in the datagrid:
'System.Collections.Specialized.StringCollection'. I guess I need some
mechanism to loop through the values of the Hostheaders property for each
object and put the content into the cell. (I also had to manually modify the
reference.cs file that contains the definition of my class to retrieve
properties instead of fields from my objects).

Any suggestions?

Morten
 
Hi,

It's simple, Hostheaders is of type StringCollection and it simply is
returning the result of ToString()

Now, you are binding it agains a IList of SiteClass instances, this class
SiteClass contains a property of type StringCollection and you want to have
a cell reflecting the values of it,

how do you want to see it , if HostHeaders is a collection?

That is your problem.

Solution:
Create a method in the code behind like:
protected string ConcatHeaders( StringCollection headers)
{
StringBuilder sb = new StringBuilder():
foreach( string s in headers )
sb.Append) sb);

return sb.ToString();
}

Then in the grid change
<%#((SiteClass)Container.DataItem).Hostheaders%>

for

<%# ConcatHeaders( ((SiteClass)Container.DataItem).Hostheaders )%>


It should do it,

Cheers,
 
Hi!

Thanks, that works. I'd like to display the values beneath eachother in the
cell. Should I simply use a "<br>" code to do this? What happens if I use
the <EditItemTemplate> method on a cell that is formatted this way?

Best regards

Morten
 
Hi,

Of course you can use <br>, a <EditItemTemplate> is simply translated in a
TD , so anything you can put inside a TD you can put inside a
EditItemTemplate.

Cheers,
 
Back
Top