This sample demonstrates the switching of data sources at run time and
shows how to reset the Grid Grouping control to its original empty state.
The sample assigns different data sources (retrieved from the Northwind Access
database using OLEDB) with a different number of levels at run time. The sample
also demonstrates how to show additional nested tables in the group drop area.
This image shows the
menu options in the
form.
This image shows a form when the option Customer Order is clicked.
The following code shows how the data set is created and how it is filled with data from the .mdb file.
private DataSet queryOleDb(string name, string query) { DataSet ds = new DataSet(); string mdbFileName = FindDatabase(@"Data\Northwind.mdb"); if (connection != null && connection.Length > 0 && query != null && query.Length > 0) { OleDbConnection conn = new OleDbConnection(String.Format(Form1.connection, mdbFileName)); OleDbDataAdapter adapter = new OleDbDataAdapter(); adapter.SelectCommand = new OleDbCommand(query, conn); adapter.Fill(ds); ds.Tables[0].TableName = name; conn.Close(); } return ds; } string FindDatabase(string mdbFileName) { for (int n = 0; n < 10; n++) { if (System.IO.File.Exists(mdbFileName)) { return mdbFileName; } mdbFileName = @"..\" + mdbFileName; } return ""; }
Here is the code used to change the data set of the Grid Grouping Control.
private void BindData(DataSet ds) { this.gridGroupingControl1.SuspendLayout(); this.gridGroupingControl1.DataSource = ds != null ? ds.Tables[0] : null;// Making columns marked 'ReadOnly' below, marks the schema as modified. //Once a schema is marked modified, its setting stays even when the source list is changed // This is by design and allows users to swap the datasource at runtime without having to worry that manual changes to the schema are lost // In order to Reset the main table descriptor and also the nested relations use the following two lines this.gridGroupingControl1.ResetTableDescriptor(); this.gridGroupingControl1.TableDescriptor.Relations.Reset();// Show group area. this.gridGroupingControl1.ShowGroupDropArea = true;// Additional table descriptors currently have to be added manually. //We plan to provide a TableDescriptor.ShowInGroupDropArea property later to make this easier AddGroupDropAreas(this.gridGroupingControl1.Table);
// Make changes to schema. See above notes that illustrate how to reset changes. SetReadOnly(this.gridGroupingControl1.Table); this.gridGroupingControl1.ResumeLayout(true); }
Here is the code to add group drop areas for the all the related tables.
private void AddGroupDropAreas(GridTable table) { Syncfusion.Grouping.RelationDescriptor rd = table.TableDescriptor.ParentRelation; if (rd != null && rd.RelationKind != Syncfusion.Grouping.RelationKind.RelatedMasterDetails) return; foreach(GridTable t in table.RelatedTables) { Console.WriteLine("AddGroupDropArea " + t.Info); gridGroupingControl1.AddGroupDropArea(t); //recurse... AddGroupDropAreas(t); } }
Here is the code used to set the table and its related table as read-only.
private void SetReadOnly(GridTable table) { table.TableDescriptor.AllowNew = false; table.TableDescriptor.AllowEdit = false; table.TableDescriptor.AllowRemove = false;//recurse... foreach(GridTable t in table.RelatedTables) { SetReadOnly(t); } }
Here is the code to invoke the Property Dialog
GroupingGridPropertyDialog dlg = new GroupingGridPropertyDialog(this.gridGroupingControl1);
dlg.Show();