Last post May 27, 2008 04:36 PM by jmosco
Feb 19, 2008 02:32 PM|tortuga|LINK
How cool are datagrids? I found out today that I can bind a custom collection to a grid and have it display with very little development pain. For example I can get a Collection<ChocolateBar> to bind, and if chocolate bar has properties,name, weight, and
chocolate type (crikey that could even be pulled from an enum), I can expect to see a grid with two columns and a nice list of all the chocolate bars in my collection. If I go for having chocolate type as an enum I can specify a bound column in my grid's markup
that will ersolve it and stick it into the grid a s a string, but here's my latest snag and I wondered if anyone was able to quickly give me a hint as to how I can solve it.
My class ChocolateBar has a public property of type Filling and Filling has properties of Name (string), Nuts (bool), Wafer (bool), and Caramel (bool).
I want my grid to contain columns;
How can I get at just the filling name when I specify the DataField in the grid's markup, I tried Filling.Name but it didn't like it, and no I don't work for Cadbury's or Hershey bar or whatever its just that time in the afternoon.
Thanks in advance,
Feb 19, 2008 10:40 PM|viscious|LINK
I've used the Filling.Name method before, something along those lines should work. Do you happen to know the error you were getting when trying that?
Feb 20, 2008 08:33 AM|tortuga|LINK
Thanks for replying, the error I get is;
A field or property with the name 'Filling.Name' was not found on the selected data source
but I definately have this as I have built the custom object before adding it to the custom collection. I'm going to go through it now, I'll post back if I find out more.
Feb 20, 2008 08:43 AM|tortuga|LINK
OK, I stil get the error if I go for one of the properties in the nested object such as Filling.Name, but if I just set the object as the datasource for the column the data in the column would display as
I just don't get why I can't use Filling.Name. I'll keep plugging at it...
Feb 20, 2008 10:30 AM|tortuga|LINK
Here's how it's done, I found the answer on http://dotnetjunkies.com/WebLog/mlorengo/archive/2005/02/02/50431.aspx where its explained much better than I could manage, but
basically you use a template column to give you the chance to cast the nested object in the current rows data item to its proper type so that you can work with its public properties, as demonstrated in my example below (sorry if you were expecting chocolate
related code examples)
Hope this is useful to folks,
May 27, 2008 04:36 PM|jmosco|LINK
My solution is
"grdListaUsuarios" runat="server" GridLines="Horizontal" BorderStyle="None" AutoGenerateColumns="False"
CellPadding="3" BorderWidth="1px" BorderColor="#E7E7FF">
<%# (((AONSusy.Vo.Seguridad.UsuarioVo)Container.DataItem).Persona).Nombre %>
"Apellido Paterno" >
<%# (((AONSusy.Vo.Seguridad.UsuarioVo)Container.DataItem).Persona).ApellidoPaterno %>
"Apellido Materno" >
<%# (((AONSusy.Vo.Seguridad.UsuarioVo)Container.DataItem).Persona).ApellidoMaterno %>
"CorreoElectronico" HeaderText="Correo Electrónico">
<%# (((AONSusy.Vo.Seguridad.UsuarioVo)Container.DataItem).EstatusVo).DescripcionCatalogo %>
"Perfil de usuario" >
<%# (((AONSusy.Vo.Seguridad.UsuarioVo)Container.DataItem).TipoUsuarioVo).DescripcionCatalogo %>