Selective Grouping in GridView

5 posts, 0 answers
  1. Frank
    Frank avatar
    86 posts
    Member since:
    Mar 2012

    Posted 03 Feb Link to this post

    I have a grid that has columns such as Job number and Admin Name. 

     

    I am grouping the rows by the Job Number. But I only want them to be grouped when there is more than one record. The user wants to see the +sign only when there is more than one admin per Job. is this possible? Can you help me find a solution for this?

     

    Thank you. 

  2. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 04 Feb Link to this post

    Hello Frank,

    Thank you for writing.

    If I understand your requirement correctly, you are trying to hide the group row element if the group has only 1 record in it. For this purpose, it is suitable to use the ViewRowFormatting event and set the GridGroupHeaderRowElement.RowInfo.Height property to 1. It is important to expand the group in order to display this single record. Here is a sample code snippet:
    public Form1()
    {
        InitializeComponent(); new RadControlSpyForm().Show();
     
        DataTable dt = new DataTable();
        dt.Columns.Add("JobNumber", typeof(string));
        dt.Columns.Add("AdminName", typeof(string));
     
        dt.Rows.Add("fp0011450", "UMB Fund Services");
        dt.Rows.Add("fp0011452", "M3Sixty Fund Services");
        dt.Rows.Add("fp0011452", "M3Sixty Fund Services");
        dt.Rows.Add("fp0011452", "M3Sixty Fund Services");
        dt.Rows.Add("fp0011452", "M3Sixty Fund Services");
        dt.Rows.Add("fp0011453", "UMB Fund Services");
        dt.Rows.Add("fp0011454", "Lavanya Funds");
        dt.Rows.Add("fp0011454", "Lavanya Funds");
     
        this.radGridView1.DataSource = dt;
        this.radGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
    }
     
     
    private void radGridView1_ViewRowFormatting(object sender, RowFormattingEventArgs e)
    {
        GridGroupHeaderRowElement groupRow = e.RowElement as GridGroupHeaderRowElement;
        if (groupRow!=null)
        {
            if (groupRow.RowInfo.ChildRows.Count == 1)
            {
                groupRow.RowInfo.Height = 1;
                groupRow.RowInfo.IsExpanded = true;
            }
            else
            {
                groupRow.RowInfo.Height = 20;
            }
        }
    }

    Note that this is just a sample approach and it may not cover all possible cases. Feel free to modify it in a way which suits your requirement best.

    I hope this information helps. Should you have further questions I would be glad to help.
     
    Regards,
    Dess
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Frank
    Frank avatar
    86 posts
    Member since:
    Mar 2012

    Posted 04 Feb in reply to Dess Link to this post

    Thank you Dess. It worked like a charm. 

    I also want the groupby column(in this case, Job number column) to show up in the row as well. 

  5. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 04 Feb Link to this post

    Hello Frank,

    Thank you for writing back. 

    In order to show the grouped column in the row as well, you should set the RadGridView.MasterTemplate.ShowGroupedColumns property to true.
    this.radGridView1.MasterTemplate.ShowGroupedColumns = true;

    I hope this information helps. If you have any additional questions, please let me know.

    Regards,
    Dess
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  6. Frank
    Frank avatar
    86 posts
    Member since:
    Mar 2012

    Posted 04 Feb in reply to Dess Link to this post

    Thank you Dess. 
Back to Top
UI for WinForms is Visual Studio 2017 Ready