This is a migrated thread and some comments may be shown as answers.

Radgrid grouping

4 Answers 156 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ron
Top achievements
Rank 1
Ron asked on 27 May 2012, 03:31 AM
Hi,

I have a grid that's grouped by days of the week. What I would like is to group by specific categories, for example; today, yesterday. All entries for today's date grouped in "Today", all entries for yesterday grouped in "Yesterday" but I'm not sure how. Currently the grid is grouped by every new date. Can I manually set this up in code? I attached a screen shot of my current grid and here is the code for my most recent attempt:
protected void RadGrid2_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        this.grdRecentActivity.MasterTableView.GroupsDefaultExpanded = false;
        if (e.Item is GridGroupHeaderItem)
        {
            GridGroupHeaderItem item = (GridGroupHeaderItem)e.Item;
            DataRowView groupDataRow = (DataRowView)e.Item.DataItem;
            DateTime currentdate = DateTime.Now.Date;
            DateTime yesterdaysDate = DateTime.Now.AddDays(-1).Date;
            DateTime dayBeforeYesterday = DateTime.Now.AddDays(-2).Date;
            DateTime activityDate = DateTime.Parse(groupDataRow.Row.ItemArray[0].ToString());
 
            if (activityDate == currentdate)
                item.DataCell.Text = "Today";
            if (activityDate == yesterdaysDate)
                item.DataCell.Text = "Yesterday";
            if (activityDate == dayBeforeYesterday)
                item.DataCell.Text = "Day Before Yesterday";
 
            //item.DataCell.Text = groupDataRow.Row.ItemArray[0];
        }
    }

4 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 27 May 2012, 07:51 AM
Hello Ron,

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
           
 
            if (e.Item is GridGroupHeaderItem)
            {
                GridGroupHeaderItem item = (GridGroupHeaderItem)e.Item;
                DateTime dt = Convert.ToDateTime((e.Item.DataItem as DataRowView).Row.ItemArray[0]);
                if (dt == DateTime.Today)
                {
                    item.DataCell.Text = "Today";
                }
                else if (dt == DateTime.Today.AddDays(-1))
                {
                    item.DataCell.Text = "Yesterday";
                }
            }
        }

<telerik:GridDateTimeColumn DataField="CustomDate" HeaderText="Date" UniqueName="CustomDate">
                   </telerik:GridDateTimeColumn>


Thanks,
Jayesh Goyani
0
Ron
Top achievements
Rank 1
answered on 27 May 2012, 12:05 PM
Thanks Jayesh for taking the time but this is similar to what I have already. What this does is if today's day match a date string in my data table it would create a group for "today". But what if the date string does not match a date in my data table? then it does not create a group for today. If you look at the picture for my grid the first date is "May 19, 2012" so I'll never get a match because May 19, 2012 has passed. What I'm trying to do is regardless of the date in my data table, I want to create 5 groups: "Today", 'Yesterday", "Day before yesterday", "this week" and "this month" then point data into the right group based on the date string. If row one has yesterday's date I want it to go into yesterdays group and so fourth. I really appreciate your assistance Jayesh.

Thanks,
Ron.
0
Jayesh Goyani
Top achievements
Rank 2
answered on 28 May 2012, 11:07 AM
Hello Ron,

For that you must have to create one more dataField in your assigned datasource.
Then Assign this datafield in to groupbyexpression.
<telerik:GridDateTimeColumn DataField="CustomDate" HeaderText="Date" UniqueName="CustomDate" GroupByExpression="DayDifference group by DayDifference">
                    </telerik:GridDateTimeColumn>

As per day difference you can assign or change the group title.

Thanks,
Jayesh Goyani
0
Ron
Top achievements
Rank 1
answered on 28 May 2012, 12:03 PM
Hi Jayesh,

Thanks again for your time. I tried your implementation but I'm missing something; what should I do here? With the GridDateTimeColumn, should I assigned a new column in my sql server data source to represent that column or can I assign it in the code behind some how? I'm not sure what to do with the information you provided, I've done tons of search to find a way to assign a value to GridDateTimeColumn in code behind but I found nothing. Here's how I implemented this in asp:
<telerik:RadGrid runat="server" ID="grdRecentActivity" DataSourceID="objActivity" Width="95%"
 Skin="Sunset" ShowHeader="false" onitemdatabound="RadGrid2_ItemDataBound" >
 <HeaderStyle CssClass="HistoryGridHeader" />
  <PagerStyle Mode="NextPrevNumericAndAdvanced"></PagerStyle>
  <MasterTableView AutoGenerateColumns="false" CssClass="GridRecentActivity" GroupLoadMode="Client">
      <GroupByExpressions>
          <telerik:GridGroupByExpression>
              <SelectFields>
                  <telerik:GridGroupByField  FieldName="CustomDate" FormatString="{0:D}" />
              </SelectFields>
              <GroupByFields>
                  <telerik:GridGroupByField FieldName="CustomDate" SortOrder="Descending" >
                  </telerik:GridGroupByField>
              </GroupByFields>
          </telerik:GridGroupByExpression>
      </GroupByExpressions>
      <Columns>
       <telerik:GridDateTimeColumn DataField="CustomDate" HeaderText="Date" UniqueName="CustomDate">
       </telerik:GridDateTimeColumn>
          <telerik:GridTemplateColumn DataField="Message" HeaderText="Message" UniqueName="Message">
              <ItemTemplate>
                  <asp:Label runat="server" id="Label1" Text='<%# Eval("Type") %>' CssClass="RecentActivityFirstColumn" />   -  
                  <asp:Label runat="server" id="lblMessage" Text='<%# Eval("Message") %>' />
              </ItemTemplate>
          </telerik:GridTemplateColumn>
       
      </Columns>
       
 
  </MasterTableView>
and here is the error I get:
Field CustomDate not found in the source table. Please check the expression syntax.
Tags
Grid
Asked by
Ron
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Ron
Top achievements
Rank 1
Share this question
or