How to access GroupByFields values in GroupHeader

1 Answer 194 Views
Grid
Margo Noreen
Top achievements
Rank 1
Iron
Veteran
Margo Noreen asked on 24 Mar 2022, 05:06 PM

I'm wondering if there is more direct way to access the datasource values or at least the DataKeyValues or GroupByField values in a grouped grid from a button click event (not ItemDataBound).

The only thing I could piece together was a bit awkard...

- loop through group header items

- get child items for the group and do one loop through that list to access a dataitem

- then access GetDataKeyValue() method to get the key value for that particular group;

Accessing controls in the header template seems pretty straightforward with the .FindControl() method.

 


protected void btnSave_Click(object sender, EventArgs e) { foreach (GridGroupHeaderItem item in gridMain.MasterTableView.GetItems(GridItemType.GroupHeader)) { string sKey= "";

string sValue = ""; // is there a better way to get to GroupByField or DataKeyName value for the current group? var childItems = item.GetChildItems(); foreach (GridDataItem child in childItems) { GridDataItem childItem = child as GridDataItem; sKey = childItem.GetDataKeyValue("SomeDataKeyName") as string; break; // only need the first one }

// get stuff from group header template

if (item.FindControl("txtSomeValue") is TextBox txtBox)
            {
                sValue = txtBox.Text;
            }

// additional code here to do stuff with retrieved values

} }


1 Answer, 1 is accepted

Sort by
0
Doncho
Telerik team
answered on 29 Mar 2022, 12:24 PM

Hi Margo,

The approach you have shared seems completely feasible to me and I am afraid there is no other more straightforward way to achieve the same. 

Yet, It might be useful if you share some more information about the current case. What is the purpose of looping through the groups? 

One alternative strategy I can think of is to get the FieldNames out of the Grid's GridGroupByExpressions and manually query the database (or the collection used as DataSource of the Grid) to get the desired values:

var groupByExpressions = RadGrid1.MasterTableView.GroupByExpressions;
foreach (GridGroupByExpression expression in groupByExpressions)
{
    foreach (GridGroupByField field in expression.GroupByFields)
    {
        var fieldName = field.FieldName;
        //query the database here
        //e.g. SELECT DISTINCT fieldName...
    }
}

Kind regards,
Doncho
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Margo Noreen
Top achievements
Rank 1
Iron
Veteran
commented on 04 Apr 2022, 05:10 PM

Hello Doncho

The "use case" in this specific situation is that we have a <GroupHeaderTemplate> set up that, in turn, contains a user-editable textbox.  Our user enters in some information into that textbox.  They can do that for one or more of the grouped rows.  Then they click a "Save" button on the page.

So, as in the sample code included above, the ultimate goal is to get the value of that textbox in the group's header area.  The value is saved back to a database where it applies to all the records in that grouping...

item.FindControl("txtConfirmationNumber") is TextBox txtBox

string s = txtBox.Text;

//save the value

Doncho
Telerik team
commented on 07 Apr 2022, 01:13 PM

Thank you for the clarification, Margo!

I am afraid that there is not much more I can help with on this case.

The GroupHeaderTemplate is designed mainly to provide flexibility in presenting some Group information. Having an input control in the GroupHeaderTemplate is a custom and not really a common scenario.

Yet, one more approach I can think of is to use the OnTextChanged event of the TextBox controls as it will fire separately for each TextBox whose value has been changed. In this event, you can easily reach just the GridGroupHeaderItem holding that particular TextBox and again execute the logic for reaching the DataKeyValue.

This approach however will not bring much to the overall implementation or performance as it only will replace looping through the GridGroupHeaderItems.

If you find it useful, here is a sample piece of code demonstrating the idea:

protected void TextBox_TextChanged(object sender, EventArgs e)
{
    var textBox = sender as TextBox;
    var item = textBox.NamingContainer as GridGroupHeaderItem;
    var childItems = item.GetChildItems();
    string sKey;
    foreach (GridDataItem child in childItems)
    {
        GridDataItem childItem = child as GridDataItem;
        sKey = childItem.GetDataKeyValue("SomeDataKeyName") as string;
        break; // only need the first one
    }
}

Tags
Grid
Asked by
Margo Noreen
Top achievements
Rank 1
Iron
Veteran
Answers by
Doncho
Telerik team
Share this question
or