Datetime column sorting, filtering and formating (conditional)

2 posts, 0 answers
  1. Maulik Patel
    Maulik Patel avatar
    36 posts
    Member since:
    Mar 2010

    Posted 11 Feb 2013 Link to this post


    We are using RadGrid to render our data in tabular format. There is one DateTime column that is going to show the data in "MMMM dd, yyyy" format (i.e. December 15, 2012). We are trying using GridDateTimeColumn and GridTemplateColumn for the same. But we are not able to fulfill our requirements.

    Requirements are :
    1. Datetime column should not show "12:00 AM" if it does not have any time information. (should show date part only) (i.e. December 15, 2012)
    2. Null or 1/1/1900 date value should be shown as Blank. (i.e. EMPTY)
    3. If time part is available, column/cell should have time part visible next to date (i.e. December 15, 2012 05:00 PM)
    4. there should be datepicker for filtering on column.

    All these should work together.

    We have tried following approaches but we are not able to do conditional formatting. We are able to go either way. Can anyone please help on this?

    Your quick reply should be really helpful.

    <rad:GridDateTimeColumn UniqueName="GridDateTimeColumnEndDate" AllowSorting="true" Visible="True" HeaderText="Ends" DataField="EndDate" SortExpression="EndDate" ReadOnly="true" AutoPostBackOnFilter="true" CurrentFilterFunction="EqualTo" ShowFilterIcon="false" DataType="System.DateTime" DataFormatString="{0:MMMM dd, yyyy}" EnableTimeIndependentFiltering="false" >
          <ItemStyle Width="260px" />

    <rad:GridTemplateColumn UniqueName="EndDate" HeaderText="EndDate" DataField="EndDate" SortExpression="EndDate" AutoPostBackOnFilter="true" CurrentFilterFunction="Contains" ShowFilterIcon="false" DataType="System.DateTime">
                    <asp:Label ID="lblend" runat="server" Text='<%# fixDate(DataBinder.Eval(Container.DataItem,"EndDate")) %>'></asp:Label>
                    <rad:RadDatePicker ID="rp" runat="server" Width="100px" SelectedDate='<%# SetEndDate(Container) %>' ClientEvents-OnDateSelected="DateSelected" >
                                    <DateInput runat="server" DateFormat="MMMM dd, yyyy" ></DateInput>
             <ItemStyle Font-Size="10pt" Width="150px" />

  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 11 Feb 2013 Link to this post


    Please take a look into the following code snippet I tried to meet your requirement using GridDateTimeColumn. Inorder to show DatePicker while filtering set ShowFilterIcon as True.

    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        if (e.Item is GridDataItem)
            GridDataItem ditem = (GridDataItem)e.Item;
            TableCell cell=(TableCell)ditem["GridDateTimeColumnEndDate"];
            // To show blank field if the date is Null or 1/1/1900
            if (cell.Text == " " || cell.Text == "January 01, 1900 12:00 AM")
                cell.Text = " ";
            //To remove the 12:00 AM
            if(cell.Text.Contains("12:00 AM"))
                DateTime value =Convert.ToDateTime(cell.Text);
                cell.Text = String.Format("{0:MMMM dd, yyyy}", value);

  3. DevCraft R3 2016 release webinar banner
Back to Top