Re: DataBinder.Eval(Container.DataItem, "date") when date might be missing

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Scott Allen (bitmask_at_[nospam)
Date: 04/16/04


Date: Fri, 16 Apr 2004 14:43:18 -0400

I'm glad you have everything working.

Try search for:
  dino esposito data binding

Dino has written a number of good articles on various places around
the web.

--
Scott
http://www.OdeToCode.com
On Fri, 16 Apr 2004 13:43:27 +0200, "bg"
<stefan_bg_edlund@hotmail.com> wrote:
>Hi Scott!
>
>I finally made it after a lot of try and error. Thanks for all the help. I
>had problems understanding the object model any ideas where I can learn more
>so I get this (including my own code :-> )?
>/Stefan
>
>The result:
>
>
>  public string GetDate(Object o)
>  {
>   string ret = "";
>
>   DataRowView drv = (DataRowView) o;
>
>   if (drv.Row.Table.Columns.Contains("date"))
>   {
>    int index = drv.Row.Table.Columns.IndexOf("date");
>    ret = drv.Row.ItemArray.GetValue(index).ToString().Substring(0, 10);
>   }
>
>   return ret;
>  }
>
>
><asp:DataGrid
> id="RssNewsGrid"
> AutoGenerateColumns="False"
> runat="server"
> AlternatingItemStyle-BackColor="#ffffff"
> HeaderStyle-BackColor="#ffffff"
> OnPageIndexChanged="ChangePaging"
> AllowPaging="True"
> Width="100%"
> Cellpadding="4"
> BorderWidth="0">
> <PagerStyle Mode="NumericPages" Visible="False" HorizontalAlign="Right" />
>  <Columns>
>    <asp:TemplateColumn>
>  <HeaderTemplate>
>  </HeaderTemplate>
>  <ItemTemplate>
>   <h2 class="<%=HeaderClass2%>"><%# DataBinder.Eval(Container.DataItem,
>"title") %></h2>
>   <em><asp:Label CssClass="txtNormal" runat="server" Text='<%#
>GetDate(Container.DataItem) %>' ' ID="Label2" NAME="Label2"/></em>
>   <asp:Label CssClass="txtNormal" runat="server"
>Text='<%#DataBinder.Eval(Container.DataItem, "description") %>' ID="Label1"
>NAME="Label1"/>
>  </ItemTemplate>
>    </asp:TemplateColumn>
>  </Columns>
></asp:DataGrid>
>
>
>Thanks again
>
>
>
>
>"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
>news:3l6t70hc62uu7kn8p3e0o113500c6oqe1d@4ax.com...
>> Well, it depends on what the DataSource is (an XmlDocument, an
>> ArrayList, a DataSet, etc).
>>
>> Something you could try is:
>>
>> public string GetDate(object o)
>> {
>>    return String.Empty;
>> }
>>
>> and set a breakpoint on the method. Then when you run with the
>> debugger you can inspect o in a quick watch window and see what type
>> the data binding is passing. Then go back and declare the method with
>> the correct type and work from there. A bit of a hack, but like I say,
>> depending on your DataSource you'll get different types of object
>> references here.
>>
>> HTH,
>> --
>> Scott
>> http://www.OdeToCode.com
>>
>>
>>
>> On Thu, 15 Apr 2004 10:36:43 +0200, "bg"
>> <stefan_bg_edlund@hotmail.com> wrote:
>>
>> >Hi Scott and thanks for the tip.
>> >
>> >I tried that yesterday but don't know what type to declare. DataItem
>seems
>> >not to exists... String wont work of couse.
>> >  public string GetDate2(string sIn)
>> >  {
>> >
>> >Thanks
>> >/Stefan
>> >
>> >
>> >"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
>> >news:ss8r70hu2tcr5dvsvi6fgj79sdhqh3oot3@4ax.com...
>> >> One solution is to call a method in your page object intead of
>> >> DataBinder.Eval, i.e.:
>> >>
>> >> <%# GetDate(Container.DataItem %>
>> >>
>> >> Them implement GetDate to look for the optional field and return
>> >> String.Empty if not present.
>> >>
>> >> --
>> >> Scott
>> >> http://www.OdeToCode.com
>> >>
>> >>
>> >> On Wed, 14 Apr 2004 18:19:20 +0200, "bg"
>> >> <stefan_bg_edlund@hotmail.com> wrote:
>> >>
>> >> >Hi!
>> >> >
>> >> >How do I check if "date" exists before using that code?
>> >> >
>> >> >I've built a RSSreader and sometimes there's a date in it and
>sometimes
>> >not.
>> >> >How can I check if it exists to avoid crash (DataBinder.Eval:
>> >> >'System.Data.DataRowView' does not contain a property with the name
>date)
>> >> >
>> >> ><asp:DataGrid
>> >> > id="RssNewsGrid"
>> >> > AutoGenerateColumns="False"
>> >> > runat="server"
>> >> > AlternatingItemStyle-BackColor="#ffffff"
>> >> > HeaderStyle-BackColor="#ffffff"
>> >> > OnPageIndexChanged="ChangePaging"
>> >> > AllowPaging="True"
>> >> > Width="100%"
>> >> > Cellpadding="4"
>> >> > BorderWidth="0">
>> >> > <PagerStyle Mode="NumericPages" Visible="False"
>HorizontalAlign="Right"
>> >/>
>> >> >  <Columns>
>> >> >    <asp:TemplateColumn>
>> >> >  <HeaderTemplate>
>> >> >  </HeaderTemplate>
>> >> >  <ItemTemplate>
>> >> >   <h2 class="<%=HeaderClass2%>"><%#
>DataBinder.Eval(Container.DataItem,
>> >> >"title") %></h2>
>> >> >   <em><%# DataBinder.Eval(Container.DataItem, "date") %>
>> >> >  </ItemTemplate>
>> >> >    </asp:TemplateColumn>
>> >> >  </Columns>
>> >> ></asp:DataGrid>
>> >> >
>> >> >
>> >> >Thank you
>> >> >/Stefan
>> >> >
>> >> >
>> >>
>> >
>>
>


Relevant Pages