1. negar
    negar avatar
    7 posts
    Member since:
    Aug 2019

    Posted 07 Aug Link to this post

    Hello, have good time
    I have a page where it's a grid.

    In this grid one of the columns is hidden where the user ID is and in the other column it fills the user name with another table in the event ItemDataBound
    But when grouping it shows the user ID while I need the name to show it.

      protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
            {

     if (e.Item is GridDataItem)// to access a row
                {
                    GridDataItem item = (GridDataItem)e.Item;
                    if (RadGrid1.GroupingEnabled)
                    {
                        item["UserFLName"].Text = LstUser.FindAll(x => x.User_ID_ == int.Parse(item["User_ID"].Text))[0].User_FName + " " + LstUser.FindAll(x =>     x.User_ID_ == int.Parse(item["User_ID"].Text))[0].User_LName;
                    }
                }

  2. Attila Antal
    Admin
    Attila Antal avatar
    429 posts

    Posted 09 Aug Link to this post

    Hi Negar,

    Please double check the DataField and DataType properties for both the "User_ID" and "UserFLName". They both bind to the User_ID field from the DataSource.

    Also, to override the Cell's text, try the PreRender event instead of the ItemDataBound. In that event handler, you will need to loop through the grid items.

    Try the following approach with a Template column. Initially evaluate the User_ID

                        <telerik:GridTemplateColumn UniqueName="MyTemplateColumn1">
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Eval("User_ID") %>''></asp:Label>
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>

    On PreRender event, if Grouping is enabled, loop through the Grid records, find the Label in the Template column and set its text to the Concatenated value of First and Last name for the User.

        protected void RadGrid1_PreRender(object sender, EventArgs e)
        {
            RadGrid grid = sender as RadGrid;
            if (grid.GroupingEnabled)
            {
                foreach (GridDataItem item in grid.Items)
                {
                    Label lbl = item["MyTemplateColumn1"].FindControl("Label1") as Label;
                    lbl.Text = string.Format("{0} {1}", item["User_FName"].Text, item["User_LName"].Text);
                }
            }
        }

     

    Kind regards, Attila Antal
    Progress Telerik

    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  3. negar
    negar avatar
    7 posts
    Member since:
    Aug 2019

    Posted 14 Aug in reply to Attila Antal Link to this post

    dear Mr. Antal

    Many thanks for your valuable response.

    I did the command you gave but when I want to group the user column it shows the user ID in the group header.
    I want the name of the user to show in the header of each group.

    If you notice the attached image, the header name of each group is the userID.

    Note that the user ID column is not displayed and Visible="False" and UserID Column is foreign key of User table.

     

  4. Attila Antal
    Admin
    Attila Antal avatar
    429 posts

    Posted 15 Aug Link to this post

    Hi Negar,

    Thank you for the details. Now I have a better understanding on this scenario.

    If you are doing this declarative in the Markup, you would do create a GroupByExpression where you can define which field should be used to group the items by (defined in the GroupByFields tag) and which fields to be displayed (defined in the SelectFields tag). See Declarative Definition

    The following example shows that the items are groupped by the ID but UserName is displayed in the GroupHeader:

    <telerik:RadGrid ID="RadGrid1" runat="server">
        <MasterTableView>
            <GroupByExpressions>
                <telerik:GridGroupByExpression>
                    <GroupByFields>
                        <telerik:GridGroupByField FieldName="ID" />
                    </GroupByFields>
                    <SelectFields>
                        <telerik:GridGroupByField FieldName="UserName" />
                    </SelectFields>
                </telerik:GridGroupByExpression>
            </GroupByExpressions>
        </MasterTableView>
    </telerik:RadGrid>

     

    If you want this to be done when Dragging a Column to be groupped, you can define GroupByExpression  for the column as well. This is described in the Column group-by expressions article.

    Example:

    <telerik:GridBoundColumn DataField="UserName"
        FilterControlAltText="Filter UserName column" HeaderText="UserName" GroupByExpression="UserName Group By ID desc"
        SortExpression="UserName" UniqueName="UserName">
    </telerik:GridBoundColumn>

     

    You can also do that programmatically in the code behind described in the Programmatic Definition article.

    This will do the for your case.

    Kind regards, Attila Antal
    Progress Telerik

    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  5. negar
    negar avatar
    7 posts
    Member since:
    Aug 2019

    Posted 17 Aug in reply to Attila Antal Link to this post

    Hi,

    Thank you very much for your guidance and accountability.

    I fully understood the topic of grouping.

    I've used generic lists for my entire project to connect to database tables.

    I have attached the codes to clarify the issue.

    I used a generic list to fill the grid, as follows:

    <telerik:RadGrid ID="RadGrid1" runat="server" ShowGroupPanel="true" AllowFilteringByColumn="true". . .>
    <GroupingSettings ShowUnGroupButton="true" GroupContinuesFormatString=""></GroupingSettings>
        <ClientSettings EnablePostBackOnRowClick="true" AllowDragToGroup="true">
        <Selecting AllowRowSelect="True" />
        </ClientSettings>
        <MasterTableView AutoGenerateColumns="False" DataKeyNames="Food_User_ID">
         <Columns>
        <telerik:GridClientSelectColumn UniqueName="SelectColumn" Visible="false" 
         ItemStyle-Width="50px" />
        <telerik:GridTemplateColumn HeaderText="Row" ItemStyle-HorizontalAlign="Center"
         HeaderStyle-Width="30px" Groupable="false">
        <ItemTemplate><%# (Container.ItemIndex+1).ToString() %> </ItemTemplate>
        </telerik:GridTemplateColumn>
        <telerik:GridBoundColumn DataField="Food_ID_" DataType="System.Int32"  HeaderText="Row"
         UniqueName="Food_ID_" Visible="false"/>        
        <telerik:GridBoundColumn ItemStyle-Width="100px" DataField="Food_Name" 
        HeaderText="Food Name"  UniqueName="Food_Name" />
        <telerik:GridBoundColumn ItemStyle-Width="100px" DataField="Food_Price"  HeaderText="Price"
        UniqueName="Food_Price" DataType="System.Int32" />
         <telerik:GridImageColumn ItemStyle-Width="100px" DataImageUrlFields="Food_Image"
         Groupable="false" DataType="System.String" UniqueName="Image" AlternateText="Food Image"
         ImageAlign="Middle" ImageHeight="60px" ImageWidth="60px" HeaderText="Image" />
         <telerik:GridBoundColumn ItemStyle-Width="100px" DataField="Food_User_ID"
         HeaderText="UserID"  UniqueName="User_ID" DataType="System.Int32" />
         <%--<telerik:GridBoundColumn ItemStyle-Width="100px" DataField="Food_User_ID"
         FilterControlAltText="Filter Food_User_ID column" HeaderText="User"
         SortExpression="Food_User_ID" UniqueName="UserLFName" DataType="System.Int32" />--%>
        <telerik:GridTemplateColumn UniqueName="MyTemplateColumn1" HeaderText="User"
         Groupable="true" GroupByExpression="Food_User_ID user group by Food_User_ID">
        <ItemTemplate>
         <asp:Label ID="Label1" runat="server" Text='<%# Eval("Food_User_ID") %>'></asp:Label>
        </ItemTemplate>
    </telerik:GridTemplateColumn>
    <telerik:GridCheckBoxColumn ItemStyle-Width="50px" DataField="Food_Status"
         DataType="System.Boolean" FilterControlAltText="Filter Food_Status column"
         HeaderText="Status" SortExpression="Food_Status" UniqueName="Food_Status" />
    </Columns>
    </MasterTableView>
    <FilterMenu></FilterMenu>
    </HeaderContextMenu>
    </telerik:RadGrid>
    01.ClsTblBase_Food Cls_Food = new ClsTblBase_Food();
    02.ClsTblBase_FoodFactory Cls_FoodF = new ClsTblBase_FoodFactory();
    03.static List<ClsTblBase_Food> LstFood = new List<ClsTblBase_Food>();
    04. 
    05.protected void Page_Load(object sender, EventArgs e)
    06.  {
    07.    if (!Page.IsPostBack)
    08.    {
    09.     SubSetData();
    10.    }
    11.     RadGrid1.DataSource = LstFood;
    12.  }
    13.private void SubSetData()
    14.  {
    15.    LstUser = Cls_UserF.GetAll();
    16.    LstFood = Cls_FoodF.GetAll();
    17.    RadGrid1.DataBind();
    18.  }
    19.protected void RadGrid1_PreRender(object sender, EventArgs e)
    20.  {
    21.    RadGrid grid = sender as RadGrid;
    22.    if (grid.GroupingEnabled)
    23.     {
    24.       foreach (GridDataItem item in grid.Items)
    25.        {
    26.          Label lbl = item["MyTemplateColumn1"].FindControl("Label1") as Label;
    27.         lbl.Text = string.Format("{0} {1}", LstUser.FindAll(x => x.User_ID_ == int.Parse(item["User_ID"].Text))[0].User_FName, LstUser.FindAll(x => x.User_ID_ == int.Parse(item["User_ID"].Text))[0].User_LName);
    28.         }
    29.      }
    30.   }

    I don't have a Username to Use for Generating the Column DataField Attribute Because of the Generic List Generator. As a result, in the PreRender event I assign the user's name from the generic list to the label. Therefore it displays the UserID in the grouping instead of the Username.
    Is there a way when grouping an event can replace the header of each group with the grouped user name?

     


  6. Attila Antal
    Admin
    Attila Antal avatar
    429 posts

    Posted 21 Aug Link to this post

    Hi Negar,

    My first suggestion is to change the data binding technique. See How to bind RadGrid properly on server-side

    I believe the main problem here is the Data Binding technique. From the code snippets you have shared I can see that Simple Data Binding is used, which is okay  for displaying data and making very basic functionality such as sorting and filtering. If it comes to a more complex scenario, we always advise using either the Advanced Data-binding (Using NeedDataSource Event) or Declarative DataSource.

     

    As next I would like to know a little bit more about the structure of the DataSource that is being assigned to the Grid. Right now, I understand that you're trying to Group by a field which is not in the DataSource but your are assigning the datasource again which has the field. Can you please clarify that part?

     

    Eventually I advise you that you open a formal support ticket and send us a runnable sample project in it which produces the error. Once we fix the problem, we will share the solution in this forum thread as well. 

     

    Kind regards, Attila Antal
    Progress Telerik

    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top