Use gridview with generic collection?

D

dgk

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

Teemu Keiski

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

dgk

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>


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.
 
T

Teemu Keiski

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top