Hi.
We're using the XamDataGrid and are trying to programmatically group the grid by a column, directly followed by expanding/collapsing certain groups. We use the following code to perform the grouping:
We then loop through the xamDataGrid.Records (which should all now be of type GroupByRecord), and mark them as either expanded or not. Both actions (the grouping and expansion/collapsing) work, but not both at the same time. The problem with the expansion/collapsing is that the records have not yet become GroupByRecords.
It seems like the sorting/grouping occurs asynchronously and we have not been able to find an event that fires when the sorting/grouping finishes. (There's a Grouped event and such, but that only fires when the grouping is triggered by the end user).
Any help is appreciated!
Lars Andreas
Hello Lars,
I have reproduced locally that situation and everything works fine with me or at least what I think you are asking.
I have created a table and group records programatically like you have done. The loop through all of them and expand and collapse them. I am not sure what is the problem so I will paste you my source code. I am also using NetAdvantage for WPF 2008 Vol. 2.
<igDP:XamDataGrid Name="xamDataGrid1"> </igDP:XamDataGrid> <Button Height="30" Name="button1" Width="64" Click="button1_Click">Group</Button> <Button Height="30" Name="button2" Width="64" Click="button2_Click">Expand</Button> <Button Height="30" Name="button3" Width="64"Click="button3_Click">Collapse</Button>
and the Code-Behind file:
public class Numbers { public int A { get; set; } public int B { get; set; } public int C { get; set; } public Numbers(int a, int b, int c) { A = a; B = b; C = c; } } ObservableCollection<Numbers> collection = new ObservableCollection<Numbers>(); public Window1() { InitializeComponent(); collection.Add(new Numbers(1, 2, 3)); collection.Add(new Numbers(5, 6, 7)); collection.Add(new Numbers(2, 3, 4)); collection.Add(new Numbers(7, 8, 9)); collection.Add(new Numbers(2, 2, 1)); collection.Add(new Numbers(5, 5, 9)); xamDataGrid1.DataSource = collection; } private void button1_Click(object sender, RoutedEventArgs e) { xamDataGrid1.FieldLayouts[0].SortedFields.Clear(); xamDataGrid1.FieldLayouts[0].SortedFields.Add(new FieldSortDescription { FieldName = "A", IsGroupBy = true }); xamDataGrid1.FieldLayouts[0].SortedFields.Add(new FieldSortDescription { FieldName = "B", IsGroupBy = true }); } private void button2_Click(object sender, RoutedEventArgs e) { foreach (var r in xamDataGrid1.Records) { r.IsExpanded = true; } } private void button3_Click(object sender, RoutedEventArgs e) { foreach (var r in xamDataGrid1.Records) { r.IsExpanded = false; } } }
I hope this helps.
Hi, and thanks for your reply!
Our problem is somewhat different. We perform the grouping and expanding in the same method (as the result of a single user action). We took your example code and copied the code from button2_Click into button1_Click:
Clicking button1 for the first time will only group the data, but not expand the groups. If you press the button once more (while the data is grouped, but not expanded) the groups will expand.
Hello,
I believe at this point it is best to contact our Developer Support department directly - they can be reached athttp://ko.infragistics.com/support/Thanks.
My colleagues and me have found a solution for this, for everyone who comes here upon search. Here it is:
1. Hook up the Grouped Event. It fires when grouping and ungrouping so you have to distinguish between the two processes.You can do that easily by checking the e.Groups.Length!= 0 -- grouping. Then use a bool value to save this state - isGrouped = true; Like this:
Initialize:
public delegate void NextPrimeDelegate();
bool isGrouped = false;
private void xamDataGrid1_Grouped(object sender, Infragistics.Windows.DataPresenter.Events.GroupedEventArgs e) { if (e.Groups.Length != 0) { isGrouped = true; xamDataGrid1.Dispatcher.BeginInvoke(new NextPrimeDelegate(ExpandRecords), null); } }
2. ExpandRecords Method -- async:
public void ExpandRecords() { foreach (DataRecord dr in xamDataGrid1.Records) { dr.IsExpanded = true; } }
3. Hook up the RecordInitialized event and handle it only when the records are grouped
private void xamDataGrid1_InitializeRecord(object sender, Infragistics.Windows.DataPresenter.Events.InitializeRecordEventArgs e) { if (isGrouped) { e.Record.IsExpanded = true; isGrouped = false; } }
Hope this is helpful to everyone.
Alex.
As a follow-up I am posting a solution to this problem. The requirement is to group records from procedural code. Here is the solution:
bool needToExpand = false;
void XamDataGrid1_RecordsInViewChanged(object sender, Infragistics.Windows.DataPresenter.Events.RecordsInViewChangedEventArgs e)
{
if (needToExpand)
foreach (Record r in this.XamDataGrid1.Records)
r.IsExpanded = true;
}
needToExpand = false;
void btnGroup_Click(object sender, RoutedEventArgs e)
FieldSortDescription fsd = new FieldSortDescription("Department", ListSortDirection.Ascending, true);
this.XamDataGrid1.FieldLayouts[0].SortedFields.Add(fsd);
needToExpand = true;
Regards,Alex.
Alex Fidanov"] Hello Christo, If I undestood you correctly, you have to loop through the child records of the parent records and expand them as well. Alex.
Hello Christo,
If I undestood you correctly, you have to loop through the child records of the parent records and expand them as well.
Thanx Alex, that worked
Here is my code on how I did it:
foreach (var r in MyXamDataGrid.Records) { r.IsExpanded = true; foreach (var c in r.ViewableChildRecords) { c.IsExpanded = true; foreach (var c1 in c.ViewableChildRecords) { c1.IsExpanded = true; } } }
Just a side note. You can for as many groupings as you like. If you have 5 columns you can add the code for 5 expansions in case the user adds some more groupings. It will just end at the lowest level.
Regards
Christo
Hi Alex.
Does your function expand the 2nd level of collapsed records as well. I have a grid that is grouped by two columns, and I need to expand both levels. The grouping is set when the form is loaded, so your first example should be good enough.
What is actually want to do is update the record after is was changed in another user control and then set the focus on the record that was selected before the update. What I do at the moment is this:
_odp = (ObjectDataProvider)(this.grid.Resources["odpPlantMap"]);
at UserControl_Loaded
and
_odp.Refresh();
Your code only expands the first level, and I need to expand the second level as well and then set the focus (active/selected) on the record in the 3 level (where the gird is then displayed.
Kind regards