Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / September 2007

Tip: Looking for answers? Try searching our database.

Use gridview with generic collection?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
dgk - 13 Sep 2007 19:53 GMT
I figured that I'd just set the datasource of a gridview to a generic
list of classx, but at runtime databind complains: "The data source
for GridView with id 'gv1' did not have any properties or attributes
from which to generate columns.  Ensure that your data source has
content."

The list (of classx) has public properties, which means rows and
columns. Ok, so the gridview can't handle it. What's a good way to
display the items in the list (of classx) and get the user to select
one?
Teemu Keiski - 13 Sep 2007 20:09 GMT
Hi,

it should be able if you really have members as properties. E.g

public class Point
{
   private float _x;
   private float _y;

public Point(float x,float y)
{
       _x = x;
       _y = y;
}

   public float X
   {
       get
       {
           return _x;
       }
   }

   public float Y
   {
       get
       {
           return _y;
       }
   }
}

default.aspx

protected void Page_Load(object sender, EventArgs e)
   {
       List<Point> points = new List<Point>();

       Point p = new Point(1, 2);
       points.Add(p);

       p = new Point(4, 6);
       points.Add(p);

       p = new Point(9, 2);
       points.Add(p);

       grid1.DataSource = points;
       grid1.DataBind();
   }

<asp:GridView ID="grid1" runat="server">

       </asp:GridView>

Your grid could also be explicit (showing two different variations)

<asp:GridView ID="grid1" runat="server" AutoGenerateColumns="false">
           <Columns>
               <asp:BoundField DataField="x" HeaderText="X coordinate" />
               <asp:TemplateField HeaderText="Y coordinate">
                   <ItemTemplate>
                       <%#Eval("Y")%>
                   </ItemTemplate>
               </asp:TemplateField>
           </Columns>

       </asp:GridView>

Signature

Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net

>I figured that I'd just set the datasource of a gridview to a generic
> list of classx, but at runtime databind complains: "The data source
[quoted text clipped - 6 lines]
> display the items in the list (of classx) and get the user to select
> one?
dgk - 14 Sep 2007 14:13 GMT
>Hi,
>
[quoted text clipped - 64 lines]
>
>        </asp:GridView>

Thanks much. I tried it (translated to VB since that's what I'm using)
and it worked. So I looked closer to see why my class, which was
roughly equivalent, didn't work. Oddly, it is something I did not
expect.

In VB, I've always used Public variables in classes rather than a
private variable and associated property declaration, unless I needed
greater control over the values coming in or going out. In fact, I
thought that the compiler just converted a public class variable into
a private variable and associated property clauses.

However, a generic collection of this class did not work with
databind:

 Friend Class CaseChoice
   Public ClientLastName As String
   Public ClientFirstName As String
   Public DocketInd As String
   Public DOB As String
 End Class

This class, with one public variable converted into a property, works
with Databind but only the one property shows on the grid:

 Friend Class CaseChoice
   Private _ClientLastName As String
   Public ClientFirstName As String
   Public DocketInd As String
   Public DOB As String
   Property ClientLastName() As String
     Get
       Return _ClientLastName
     End Get
     Set(ByVal value As String)
       _ClientLastName = value
     End Set
   End Property
   Public Sub New(ByVal InCLN As String)
     _ClientLastName = InCLN
   End Sub
 End Class

This class works fine even though some properties are readonly and
others aren't:

 Friend Class CaseChoice
   Private _ClientLastName As String
   Private _ClientFirstName As String
   Private _DocketInd As String
   Private _DOB As String

   Property ClientLastName() As String
     Get
       Return _ClientLastName
     End Get
     Set(ByVal value As String)
       _ClientLastName = value
     End Set
   End Property

   Property ClientFirstName() As String
     Get
       Return _ClientFirstName
     End Get
     Set(ByVal value As String)
       _ClientFirstName = value
     End Set
   End Property

   ReadOnly Property DOB() As String
     Get
       Return _DOB
     End Get
   End Property

   ReadOnly Property DocketInd() As String
     Get
       Return _DocketInd
     End Get
   End Property

   Public Sub New(ByVal InCLN As String, ByVal InCFN As String, ByVal
InDI As String, ByVal InDOB As String)
     _ClientLastName = InCLN
     _ClientFirstName = InCFN
     _DocketInd = InDI
     _DOB = InDOB
   End Sub
 End Class


Very odd. I would thing that functionally a public variable would be
exactly the same as a property. Oh well, something new to look into. I
guess I'll have to look at the IL.
Teemu Keiski - 15 Sep 2007 07:44 GMT
Hi,

databinding uses DataBinder.Eval (or just Eval ) behind the secenes and it
looks only for properties (read-only is enough for displaying). They aren't
exactly the same as public members (fields). Properties are closer to
methods than fields are.

Signature

Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net

>>Hi,
>>
[quoted text clipped - 158 lines]
> exactly the same as a property. Oh well, something new to look into. I
> guess I'll have to look at the IL.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.