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

Grouping header is null?

3 Answers 88 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Mark Smith
Top achievements
Rank 2
Mark Smith asked on 15 Sep 2011, 05:58 AM
Hi

We're trying to group by a field called CompanyName. The grid seems to show the group but it creates a group per record. I tried hooked into the GroupSummaryEvaluate event and when I look at the e.Value it's null?

If I remove the grouping I can see that CompanyName has values

Why is it showing "CompanyName : " instead of "CompanyName : Some Company Name" ?

3 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 15 Sep 2011, 08:16 AM
Hello Mark,

Could you please tell me what is the type for the CompanyName? string? is it a data bound column?

For more info on grouping please take a look at this help article.

If you are still having problems, please create a small sample and post it here and i will do my best to help you solve this issue.

Best Regards,
Emanuel Varga
0
Renate
Top achievements
Rank 1
answered on 06 Jan 2012, 04:00 PM
Hello Emanuael,

I have a similar problem as Mark, but for custom grouping only:

1. grid.EnableCustomGrouping= true;

2. in grid_CustomGrouping  a new GroupKey is set for 1 column because I want to sort by another (hidden) column:
   
    e.GroupKey = <value_of_some_other_column>;

3. In the grid_GroupSummaryEvaluate event

    a) e.Value and e.Group.Header are set for non-custom-group columns but
    b) both are null for the custom-group column, so the group header only consists of the column name plus ':',
         e.g. "CompanyName : " instead of "CompanyName : Some Company Name"
  
Is there anything else I have to do in the grid_CustomGrouping event?

Hope you can help me with ths

best regards

renu


0
Julian Benkov
Telerik team
answered on 11 Jan 2012, 04:58 PM
Hi Renate,

In this custom grouping scenario you can use e.Group.Key value to format the header in GorupSummaryEvaluate event handler. Here is a codebase of our Demo application for custom grouping:

public partial class Form1 : ExamplesForm
    {
        public Form1()
        {
            InitializeComponent();
 
            this.radGridView.EnableGrouping = true;
            this.radGridView.EnableCustomGrouping = true;
            this.radGridView.ShowGroupedColumns = true;
 
            this.radGridView.GroupSummaryEvaluate += new GroupSummaryEvaluateEventHandler(radGridView_GroupSummaryEvaluate);
            this.radGridView.ViewRowFormatting += new RowFormattingEventHandler(radGridView_ViewRowFormatting);
        }
 
        private void SetConditions()
        {
            ConditionalFormattingObject c3 = new ConditionalFormattingObject("Grey, applied to cell", ConditionTypes.NotEqual, "Germany", "", false);
            c3.RowBackColor = Color.FromArgb(169, 183, 201);
            c3.CellBackColor = Color.FromArgb(169, 183, 201);
            this.radGridView.Columns["Country"].ConditionalFormattingObjectList.Add(c3);
 
            ConditionalFormattingObject c1 = new ConditionalFormattingObject("Blue , applied to cell", ConditionTypes.Equal, "Germany", "", false);
            c1.RowBackColor = Color.FromArgb(163, 191, 226);
            c1.CellBackColor = Color.FromArgb(163, 191, 226);
            this.radGridView.Columns["Country"].ConditionalFormattingObjectList.Add(c1);
 
            ConditionalFormattingObject c2 = new ConditionalFormattingObject("LightPurple, applied to cell", ConditionTypes.Equal, "France", "", false);
            c2.RowBackColor = Color.FromArgb(212, 210, 241);
            c2.CellBackColor = Color.FromArgb(212, 210, 241);
            this.radGridView.Columns["Country"].ConditionalFormattingObjectList.Add(c2);
        }
 
        private void radGridView1_CustomGrouping(object sender, GridViewCustomGroupingEventArgs e)
        {
            if (this.UseDefaultGrouping(e.Level))
            {
                e.Handled = false;
                return;
            }
 
            int color = Color.White.ToArgb();
            int columnIndex = this.radGridView.Columns["Country"].Index;
            ConditionalFormattingObjectCollection conditions = this.radGridView.Columns[columnIndex].ConditionalFormattingObjectList;
 
            for (int i = 0; i < conditions.Count; i++)
            {
                if (conditions[i].Evaluate(e.Row, this.radGridView.Columns["Country"]))
                {
                    color = conditions[i].CellBackColor.ToArgb();
                }
            }
 
            e.GroupKey = color;
        }
 
        private bool UseDefaultGrouping(int level)
        {
            GroupDescriptor groupDescriptor = this.radGridView.GroupDescriptors[level];
            for (int i = 0; i < groupDescriptor.GroupNames.Count; i++)
            {
                if (groupDescriptor.GroupNames[i].PropertyName.Equals("Country", StringComparison.InvariantCultureIgnoreCase))
                {
                    return false;
                }
            }
 
            return true;
        }
 
        void radGridView_ViewRowFormatting(object sender, RowFormattingEventArgs e)
        {
            GridViewGroupRowInfo groupRow = e.RowElement.RowInfo as GridViewGroupRowInfo;
            if (groupRow != null && groupRow.Group.Key is int)
            {
                e.RowElement.BackColor = Color.FromArgb((int)groupRow.Group.Key);
            }
        }
 
        void radGridView_GroupSummaryEvaluate(object sender, GroupSummaryEvaluationEventArgs e)
        {
            if (this.UseDefaultGrouping(e.Group.Level))
            {
                return;
            }
 
            e.FormatString = this.GetColorName(e.Group.Key.ToString());
        }
 
        private string GetColorName(string p)
        {
            switch (p)
            {
                case "-6045726":
                    return "Blue";
                case "-5654583":
                    return "Grey";
                case "-2829583":
                    return "LightPurple";
                default:
                    return "White";
            }
        }
 
        private void OnFormLoad(object sender, EventArgs e)
        {
            this.customersTableAdapter.Fill(this.nwindRadGridView.Customers);
 
            this.SetConditions();
            this.radGridView.GroupDescriptors.Expression = "Country";
 
            this.SelectedControl = this.radGridView;
        }
 
        protected override string GetExampleDefaultTheme()
        {
            return "ControlDefault";
        }
    }

I hope this helps. Let me know if you need further assistance.

Greetings,
Julian Benkov
the Telerik team

SP1 of Q3’11 of RadControls for WinForms is available for download (see what's new).

Tags
GridView
Asked by
Mark Smith
Top achievements
Rank 2
Answers by
Emanuel Varga
Top achievements
Rank 1
Renate
Top achievements
Rank 1
Julian Benkov
Telerik team
Share this question
or