Hi,
Thanks for the reply.
I am binding the GridView to a generic list of TimeSpentTimeStrings,
List<TimeSpentTimeStrings>.
The TimeSpentTimeStrings class looks like this.
public class TimeSpentTimeStrings
{
public DateTime Date { get; set; }
public TimeString Direct { get; set; }
public TimeString Indirect { get; set; }
public TimeString Travel { get; set; }
public TimeString Notes { get; set; }
}
I have been able to bind it, but only if I use TemplateFields for the
TimeString properties. The DateTime field binds with a BoundFields. I
have pasted the code below. I am wondering if there are changes that I
can make to either the TimeString class or the TimeSpentTimeStrings
class so that I could bind the TimeString fields to the Gridview just
as easily as the DateTime fields bind to the GridView.
I think I am getting into some more advanced territory that has not
been covered in the few books I have read about ASP.NET and C#. If
anyone out there can recommond some good reading material that covers
this sort of thing that would be great.
Thanks Again,
Paul
protected void Page_Load(object sender, EventArgs e)
{
List<DataAccess.TimeSpentTimeStrings> list =
DataAccess.AtAGlanceData.SelectTimeSpentTimeStrings(37, new DateTime
(2009, 3, 20));
grd.DataSource = list;
grd.DataBind();
}
<asp:GridView ID="grd" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:BoundField DataField="Date" HeaderText="Date" />
<asp:TemplateField HeaderText="Direct">
<ItemTemplate>
<%# ((TimeString)Eval("Direct")).ToString() %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Indirect">
<ItemTemplate>
<%# ((TimeString)Eval("Indirect")).ToString()
%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Travel">
<ItemTemplate>
<%# ((TimeString)Eval("Travel")).ToString() %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Notes">
<ItemTemplate>
<%# ((TimeString)Eval("Notes")).ToString() %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
On Apr 21, 3:53 am, Cerebrus <[email protected]> wrote:
> What datasource are you binding the GridView to? Ideally, you would
> bind the GridView to an IEnumerable datasource, and each item
> contained within that datasource would expose certain properties.
> These properties would then be accessible to the BoundFields.
>
> For instance,
>
> I bind a GridView to a generic List<Employee>. Each Employee object
> has properties such as EmpID, Name, Designation. I can then use
> BoundFields to bind to these properties.
>
> So, in context of your question, how does the TimeString class map to
> the datasource bound to your GridView? Is it exposed as a property of
> that datasource?
>
> On Apr 20, 10:12 pm, Paul <[email protected]> wrote:
>
>
>
> > Hello All,
>
> > I am trying to bind data to a GridView, but columns of type
> > "TimeString" don't render if I try to do the binding programmatically.
> > (Binding the data programmatically makes more sense for the page I am
> > using it in). The definition of the TimeString class is pasted below
> > my sig.
>
> > The only way I can make the TimeString Columns render in the GridView
> > is by using declarative data binding with a TemplateField as follows:
>
> > <asp:TemplateField HeaderText="Direct">
> > <ItemTemplate>
> > <%# ((TimeString)Eval("Direct")).ToString() %>
> > </ItemTemplate>
> > </asp:TemplateField>
>
> > If I use a BoundField with declarative binding the column renders, but
> > it is the name of the class that renders. When I bind it
> > programmatically the column doesn't render at all.
>
> > Is there a better way to design the TimeString class so that columns
> > of type TimeString render as easily as any other type, say the
> > DateTime type or the string type?
>
> > Thanks in advance for your help,
>
> > Paul
>
> > public class TimeString
> > {
> > public string HourPart { get; set; }
> > public string MinutePart { get; set; }
>
> > public TimeString(int timeSpanInMinutes)
> > {
> > int h = timeSpanInMinutes / 60;
> > int m = timeSpanInMinutes % 60;
>
> > this.HourPart = h.ToString().PadLeft(2, '0');
> > this.MinutePart = m.ToString().PadLeft(2, '0');
> > }
>
> > public string ToString()
> > {
> > return this.HourPart + ":" + this.MinutePart;
> > }
>
> > public TimeString Add(TimeString ts)
> > {
> > return new TimeString(int.Parse(this.HourPart) * 60 +
> > int.Parse(ts.HourPart) * 60 + int.Parse(this.MinutePart) + int.Parse
> > (ts.MinutePart));
> > }
> > }- Hide quoted text -
>
> - Show quoted text -