The datasource of my XamDataGrid is a DataTable. I am doing
_xamDataGrid .DataSource = new ListCollectionView(Table.NewView());
with this my XamDataGrid is working fine, each record in my XamDataGrid is a DataRowView but not a collection.
Using the "InitializeRecord" event of XamDataGrid I am able to show expansion indicator to all the records in XamDataGrid though each record is not a collection.
void _gridInfragistics_InitializeRecord(object sender, Infragistics.Windows.DataPresenter.Events.InitializeRecordEventArgs e) { e.Record.ExpansionIndicatorVisibility = Visibility.Visible; }
Now in the "RecordExpanded" event of a particular record I am getting the corresponding child data from external service which is again a DataTable. I want the child data rows to be appearing in XamDataGrid as child records of the corresponding parent.
Is there a way to achieve this. Thanks in advance.
Yeah, yesterday I finally figured out how to do that.
The trickiest thing is that I had to add property containing child records to parent's field layout to get expansion indicator visible.
Thanks.
Hello,
You can use ObservableCollection of Items, which has ObservableCollection as Property in order to get hierarchy.
Really, do I have to use datasets to get hierarchy in xamDataGrid? It is not suitable at all.
Can I use two object list (both has common key) instead of DataSet with two tables ? thx.
David
The XamDataGrid is a bound control and will not allow you to add records to it manually. However, you can add it to the datasource itself. To get around this issue, I created a dataSet with two tables that have a DataRelation. The first is the main table, and the second is the child table which does not get populated yet. Record expansion indicators will automatically be shown due to the data relation. Then just handle the record expanded and get the data the same way that you are and then just add them to the second table and the grid will automatically update.
Here is a simple code snippet of the dataset getting created with two tables and a relation and then adding to the table.
DataSet GridDataSet; void MainWindow_Loaded(object sender, RoutedEventArgs e) { GridDataSet = makeHrDataSet(); this.xamDataGrid1.DataSource = GridDataSet.Tables["Table1"].DefaultView; this.xamDataGrid1.RecordExpanded += new EventHandler<Infragistics.Windows.DataPresenter.Events.RecordExpandedEventArgs>(xamDataGrid1_RecordExpanded);
}
void xamDataGrid1_RecordExpanded(object sender, Infragistics.Windows.DataPresenter.Events.RecordExpandedEventArgs e) { if (e.Record.HasChildren) return;
string key = ((DataRecord)e.Record).Cells["Col_1"].Value.ToString(); //I am manually adding records. You can call the service to get the data. Then just add them to the table. for(int i=0; i<5; i++) {
DataRow dr = GridDataSet.Tables["Table2"].NewRow(); dr[0] = key; dr[1] = "WPF " + i.ToString(); dr[2] = "Silverlight "+ i.ToString(); dr[3] = "WinForms "+ i.ToString(); GridDataSet.Tables["Table2"].Rows.Add(dr); } }
private DataSet makeHrDataSet() { DataSet objDs = new DataSet(); DataTable objTable1 = new DataTable("Table1"); DataTable objTable2 = new DataTable("Table2"); DataRow objRow; objTable1.Columns.Add("Col_1", typeof(int)); objTable1.Columns.Add("Col_2", typeof(string)); objTable1.Columns.Add("Col_3", typeof(string)); objTable1.Columns.Add("Col_4", typeof(string));
objTable2.Columns.Add("Col_1", typeof(int)); objTable2.Columns.Add("Col_2", typeof(string)); objTable2.Columns.Add("Col_3", typeof(string)); objTable2.Columns.Add("Col_4", typeof(string));
for (int i = 0; i < 20; i++) { objRow = objTable1.NewRow(); objRow[0] = i; objRow[1] = "Hello: " + i.ToString(); objRow[2] = "Ciao: " + i.ToString(); objRow[3] = "Hola: " + i.ToString(); objTable1.Rows.Add(objRow); }
objDs.Tables.Add(objTable1); objDs.Tables.Add(objTable2);
objDs.Relations.Add("Table1_Table2_FK", objDs.Tables[0].Columns[0], objDs.Tables[1].Columns[0]);
return objDs;
I hope this helps.