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.