This sample demonstrates the switching of datasources at runtime and how to reset the GridGroupingControl to its original empty state.
The sample assigns different datasources (retrieved from the NorthWind MS Access database using OleDb) with different number of levels at runtime.The sample also demonstrates how to show additional nested tables in the GroupDropArea.
This image shows how the sample looks when it is executed.
Rebind
This image shows the Menu options in the form.
Rebind-Menu
This image shows a form when the option “Customer Order ” is clicked.
Rebind-Data
The following code shows how the Dataset is created and how it is filled with the data from the mdbfile from “NorthWind”.
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 to change the dataset of the GridGroupingControl.
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 how to reset changes
SetReadOnly(this.gridGroupingControl1.Table);
this.gridGroupingControl1.ResumeLayout(true);
}
Here is the code to add GroupDropAreas 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 to set the table and its related table as Readonly.
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 PropertyDialogWindow during runtime.
GroupingGridPropertyDialog dlg = new GroupingGridPropertyDialog(this.gridGroupingControl1);
dlg.Show();