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

Getting a group value

8 Answers 135 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Guy
Top achievements
Rank 1
Guy asked on 19 Jan 2012, 05:22 PM
Afternoon,

I have created a table which shows sales orders by agent. The items sold can one of 9 types and appear multiple times. What I would like to know is what is the best way of getting that group value for each type?

I currently have the below code which when grouped displays the group value.

private void OrderHistory_GroupSummaryEvaluate(object sender, GroupSummaryEvaluationEventArgs e)
        {
            decimal totalRevenue = 0;
   
            foreach(GridViewRowInfo row in e.Group)
            {
                totalRevenue += (decimal)row.Cells["RevenueValue"].Value;
            }
              
            e.FormatString = String.Format("{0} : £{1:0.00}", e.Value, totalRevenue);
        }

My initial thought would be to try and get at those values but I don't think that would be the correct way of doing it. I think I need to use the foreach section to tally up the values but I am unsure as to how to do that for each type.

A point in the right direction would be greatly appreciated!

Guy

8 Answers, 1 is accepted

Sort by
0
Julian Benkov
Telerik team
answered on 24 Jan 2012, 02:42 PM
Hi Guy,

Another option is to use the Evaluate method of DataGroup to evaluate a string expression:

using System;
using System.Data;
using System.Windows.Forms;
using Telerik.WinControls.UI;
 
namespace Lab
{
    public partial class GroupByGrid : Form
    {
        public GroupByGrid()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            DataTable table = new DataTable();
            table.Columns.Add("Name", typeof(string));
            table.Columns.Add("Group", typeof(string));
            table.Columns.Add("RevenueValue", typeof(decimal));
 
            table.Rows.Add("Product1", "A", 100.11);
            table.Rows.Add("Product2", "A", 200.22);
            table.Rows.Add("Product3", "B", 300.33);
            table.Rows.Add("Product4", "B", 400.44);
            table.Rows.Add("Product5", "B", 500.55);
            table.Rows.Add("Product6", "C", 600.66);
            table.Rows.Add("Product7", "C", 700.77);
            table.Rows.Add("Product8", "D", 800.88);
            table.Rows.Add("Product9", "D", 900.99);
            table.Rows.Add("Product10", "D", 1000.00);
 
            radGridView1.DataSource = table;
            radGridView1.GroupSummaryEvaluate += new Telerik.WinControls.UI.GroupSummaryEvaluateEventHandler(radGridView1_GroupSummaryEvaluate);
            radGridView1.GroupDescriptors.Add("Group", System.ComponentModel.ListSortDirection.Ascending);
        }
 
        void radGridView1_GroupSummaryEvaluate(object sender, Telerik.WinControls.UI.GroupSummaryEvaluationEventArgs e)
        {
            e.FormatString = String.Format("{0} : £{1:0.00}", e.Value, e.Group.Evaluate("SUM(RevenueValue)"));
        }
    }
}

All the best,
Julian Benkov
the Telerik team

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

0
Guy
Top achievements
Rank 1
answered on 24 Jan 2012, 04:25 PM
Afternoon Julian,

Many thanks for your reply.

I feel I might not have explained myself very so I apologise. What I would like to do is get the group values so that I can store them for other parts of my project. Using the example above how would I go about assigning the SUM values of each Group to say 4 sepearate TextBoxes? So there would essentially be a box showing the SUM Total for A,B,C and D and when the data is updated the changes are then reflected in those TextBoxes.

Regards,

Guy
0
Julian Benkov
Telerik team
answered on 27 Jan 2012, 12:24 PM
Hi Guy,

You can create a Dictionary based on group key to store related textboxes and refresh values when the data or view is changed. Here is an extended example:

public partial class GroupByGrid : Form
{
    private Dictionary<object, TextBox> sumTextBoxes = new Dictionary<object, TextBox>();
 
    public GroupByGrid()
    {
        InitializeComponent();
    }
 
    private void Form1_Load(object sender, EventArgs e)
    {
        sumTextBoxes.Add("A", new TextBox());
        sumTextBoxes.Add("B", new TextBox());
        sumTextBoxes.Add("C", new TextBox());
        sumTextBoxes.Add("D", new TextBox());
 
        //here is code adding textbox controls to form and set its position or add to Dictionary predifined text boxes.
         
 
        DataTable table = new DataTable();
        table.Columns.Add("Name", typeof(string));
        table.Columns.Add("Group", typeof(string));
        table.Columns.Add("RevenueValue", typeof(decimal));
 
        table.Rows.Add("Product1", "A", 100.11);
        table.Rows.Add("Product2", "A", 200.22);
        table.Rows.Add("Product3", "B", 300.33);
        table.Rows.Add("Product4", "B", 400.44);
        table.Rows.Add("Product5", "B", 500.55);
        table.Rows.Add("Product6", "C", 600.66);
        table.Rows.Add("Product7", "C", 700.77);
        table.Rows.Add("Product8", "D", 800.88);
        table.Rows.Add("Product9", "D", 900.99);
        table.Rows.Add("Product10", "D", 1000.00);
 
        radGridView1.DataSource = table;
        radGridView1.GroupSummaryEvaluate += new Telerik.WinControls.UI.GroupSummaryEvaluateEventHandler(radGridView1_GroupSummaryEvaluate);
        radGridView1.GroupDescriptors.Add("Group", System.ComponentModel.ListSortDirection.Ascending);
    }
 
    void radGridView1_GroupSummaryEvaluate(object sender, Telerik.WinControls.UI.GroupSummaryEvaluationEventArgs e)
    {
        object sum = e.Group.Evaluate("SUM(RevenueValue)");
 
        e.FormatString = String.Format("{0} : ?{1:0.00}", e.Value, sum);
 
        TextBox textBox = null;
        sumTextBoxes.TryGetValue(((object[])e.Group.Key)[0], out textBox);
        if (textBox != null)
        {
            textBox.Text = sum.ToString();
        }
         
    }
}

I hope this helps.

Kind regards,
Julian Benkov
the Telerik team

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

0
Guy
Top achievements
Rank 1
answered on 27 Jan 2012, 01:28 PM
Afternoon Julian,

That looks like it's going to solve my problem, however, I cannot see how I get the values into my textboxes. I have already on my form 4 textboxes called ATextBox, BTextBox, CTextBox and DTextBox.

I've not worked with Dictionaries before so I apologise if it's an obvious mistake on my part.

Many thanks,

Guy
0
Julian Benkov
Telerik team
answered on 31 Jan 2012, 03:03 PM
Hello Guy,

You can get the values from a Dictionary using the TryGetValue method, which will return a value if the key exists in the Dictionary. Here is a detailed description of this API

Do not hesitate to contact us if you have further questions or issues.

Greetings,
Julian Benkov
the Telerik team

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

0
Guy
Top achievements
Rank 1
answered on 31 Jan 2012, 04:22 PM
Hello Julian,

I'm obviously not understanding, I apologise. Isn't the supplied code already using TryGetValue?

Am I correct in thinking I need to do something where you've written

//here is code adding textbox controls to form and set its position or add to Dictionary predifined text boxes.

I am using predefined textboxes so how do I add them to the dictionary? Also what is the function of

sumTextBoxes.Add("A", new TextBox());
sumTextBoxes.Add("B", new TextBox());
sumTextBoxes.Add("C", new TextBox());
sumTextBoxes.Add("D", new TextBox());

Again I apologise for my lack of understanding and am grateful for your help!

Regards,

Guy
0
Accepted
Julian Benkov
Telerik team
answered on 02 Feb 2012, 12:45 PM
Hi Guy,

Yes, my code is using TryGetValue and my last reply was just aiming to provide additional information about what the TryGetValue does. In short, this property allows you to retrieve a TextBox instance that you can use further to set its Text.

As to my comment, it aims to clarify that my code snippet:
sumTextBoxes.Add("A", new TextBox());
sumTextBoxes.Add("B", new TextBox());
sumTextBoxes.Add("C", new TextBox());
sumTextBoxes.Add("D", new TextBox());

uses new TextBox instances for demonstration purposes. However, I suppose that you already have your TextBoxes added to your form, so you just need to add their instances to the Dictionary, instead of using "new TextBox()" instances. Let's say that you define your TextBox instances programmatically, here is how you can add them to your Dictionary:
private void Form1_Load(object sender, EventArgs e)
{
    TextBox textBox = new TextBox();
    this.Controls.Add(textBox);
    textBox.Size = new Size(100, 20);
    textBox.Location = new Point(10, 10);
    sumTextBoxes.Add("A", textBox);
     
    textBox = new TextBox();
    this.Controls.Add(textBox);
    textBox.Size = new Size(100, 20);
    textBox.Location = new Point(10, 40);
    sumTextBoxes.Add("B", textBox);
     
    textBox = new TextBox();
    this.Controls.Add(textBox);
    textBox.Size = new Size(100, 20);
    textBox.Location = new Point(10,70);
    sumTextBoxes.Add("C", textBox);
     
    textBox = new TextBox();
    this.Controls.Add(textBox);
    textBox.Size = new Size(100, 20);
    textBox.Location = new Point(10, 100);
    sumTextBoxes.Add("D", textBox);
     
    DataTable table = new DataTable();
    table.Columns.Add("Name", typeof(string));
    table.Columns.Add("Group", typeof(string));
    table.Columns.Add("RevenueValue", typeof(decimal));
 
    table.Rows.Add("Product1", "A", 100.11);
    table.Rows.Add("Product2", "A", 200.22);
    table.Rows.Add("Product3", "B", 300.33);
    table.Rows.Add("Product4", "B", 400.44);
    table.Rows.Add("Product5", "B", 500.55);
    table.Rows.Add("Product6", "C", 600.66);
    table.Rows.Add("Product7", "C", 700.77);
    table.Rows.Add("Product8", "D", 800.88);
    table.Rows.Add("Product9", "D", 900.99);
    table.Rows.Add("Product10", "D", 1000.00);
 
    radGridView1.DataSource = table;
    radGridView1.GroupSummaryEvaluate += new Telerik.WinControls.UI.GroupSummaryEvaluateEventHandler(radGridView1_GroupSummaryEvaluate);
    radGridView1.GroupDescriptors.Add("Group", System.ComponentModel.ListSortDirection.Ascending);
}

If you continue to experience an issue with this solution, please describe it in details, so that we can assist you further.

Thank you for your time and cooperation.

Greetings,
Julian Benkov
the Telerik team

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

0
Guy
Top achievements
Rank 1
answered on 02 Feb 2012, 02:37 PM
Hi Julian,

Thanks again for your patience and help.

I now understand and have it working so thank you. I think I was running into problems as I was RadTextBoxes and not just TextBoxes so I quickly changed the code and now all is working.

Again, thank you for your help!

Regards,

Guy
Tags
GridView
Asked by
Guy
Top achievements
Rank 1
Answers by
Julian Benkov
Telerik team
Guy
Top achievements
Rank 1
Share this question
or