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
} }