In this topic, you will learn how to create ColumnSet programmatically.
To create ColumnSet programmatically
- Bind WebGrid to AccessDataSource.
- Retrieve WebGrid's structure In the PrepareDataBinding event of the grid, you retrieve the structure of the grid.
C# Copy Code protected void WebGrid1_PrepareDataBinding(object sender, ISNet.WebUI.WebGrid.DataSourceEventArgs e) { WebGrid1.RootTable.ColumnSetSettings.RowCount = 3; CreateColumnSet(); }
- Create a function and name it CreateColumnSet, then Insert these codes.
C# Copy Code WebGrid1.RootTable.RowLayout = RowLayoutMode.ColumnSet;
WebGridColumnSet oColumnSet = new WebGridColumnSet();
oColumnSet.Caption = "Personal Information";
oColumnSet.Name = "ColumnSet0";
oColumnSet.ColumnCount = 3;WebGrid1.RootTable.ColumnSets.Add(oColumnSet);
WebGridRowLayoutCollection oRowLayouts = oColumnSet.Layout;
WebGridRowLayout oRowLayout;
oRowLayout = new WebGridRowLayout();
oRowLayout.Col = 0;
oRowLayout.Row = 0;
oRowLayout.ColumnMember = "Photo";
oRowLayout.RowSpan = 3;
oRowLayouts.Add(oRowLayout);
oRowLayout = new WebGridRowLayout();
oRowLayout.Spanned = RowLayoutSpan.Column;
oRowLayout.Col = 0;
oRowLayout.Row = 1;
oRowLayouts.Add(oRowLayout);oRowLayout = new WebGridRowLayout();
oRowLayout.Spanned = RowLayoutSpan.Column;
oRowLayout.Col = 0;
oRowLayout.Row = 2;
oRowLayouts.Add(oRowLayout);oRowLayout = new WebGridRowLayout();
oRowLayout.Col = 1;
oRowLayout.Row = 0;
oRowLayout.ColumnMember = "FirstName";
oRowLayouts.Add(oRowLayout);oRowLayout = new WebGridRowLayout();
oRowLayout.Col = 1;
oRowLayout.Row = 1;
oRowLayout.ColumnMember = "Title";
oRowLayouts.Add(oRowLayout);oRowLayout = new WebGridRowLayout();
oRowLayout.Col = 1;
oRowLayout.Row = 2;
oRowLayout.ColumnMember = "BirthDate";
oRowLayouts.Add(oRowLayout);oRowLayout = new WebGridRowLayout();
oRowLayout.Col = 2;
oRowLayout.Row = 0;
oRowLayout.ColumnMember = "LastName";
oRowLayouts.Add(oRowLayout);oRowLayout = new WebGridRowLayout();
oRowLayout.Col = 2;
oRowLayout.Row = 1;
oRowLayout.ColumnMember = "TitleOfCourtesy";
oRowLayouts.Add(oRowLayout);oRowLayout = new WebGridRowLayout();
oRowLayout.Col = 2;
oRowLayout.Row = 2;
oRowLayout.ColumnMember = "HireDate";
oRowLayouts.Add(oRowLayout);//Contact Information
WebGridColumnSet oColumnSet1 = new WebGridColumnSet();
oColumnSet1.Caption = "Contact Information";
oColumnSet1.Name = "ColumnSet1";
oColumnSet1.ColumnCount = 3;WebGrid1.RootTable.ColumnSets.Add(oColumnSet1);
WebGridRowLayoutCollection oRowLayouts1 = oColumnSet1.Layout;
WebGridRowLayout oRowLayout1;
oRowLayout1 = new WebGridRowLayout();
oRowLayout1.Col = 0;
oRowLayout1.Row = 0;
oRowLayout1.ColumnMember = "Address";
oRowLayouts1.Add(oRowLayout1);oRowLayout1 = new WebGridRowLayout();
oRowLayout1.Col = 1;
oRowLayout1.Row = 0;
oRowLayout1.ColumnMember = "City";
oRowLayouts1.Add(oRowLayout1);oRowLayout1 = new WebGridRowLayout();
oRowLayout1.Col = 2;
oRowLayout1.Row = 0;
oRowLayout1.RowSpan = 3;
oRowLayout1.ColumnMember = "Country";
oRowLayouts1.Add(oRowLayout1);oRowLayout1 = new WebGridRowLayout();
oRowLayout1.Col = 0;
oRowLayout1.Row = 1;
oRowLayout1.ColumnMember = "PostalCode";
oRowLayouts1.Add(oRowLayout1);oRowLayout1 = new WebGridRowLayout();
oRowLayout1.Col = 1;
oRowLayout1.Row = 1;
oRowLayout1.ColumnMember = "Region";
oRowLayouts1.Add(oRowLayout1);oRowLayout1 = new WebGridRowLayout();
oRowLayout1.Spanned = RowLayoutSpan.Column;
oRowLayout1.Col = 2;
oRowLayout1.Row = 1;
oRowLayouts1.Add(oRowLayout1);oRowLayout1 = new WebGridRowLayout();
oRowLayout1.Col = 0;
oRowLayout1.Row = 2;
oRowLayout1.ColumnMember = "HomePhone";
oRowLayouts1.Add(oRowLayout1);oRowLayout1 = new WebGridRowLayout();
oRowLayout1.Col = 1;
oRowLayout1.Row = 2;
oRowLayout1.ColumnMember = "Extension";
oRowLayouts1.Add(oRowLayout1);
oRowLayout1 = new WebGridRowLayout();
oRowLayout1.Spanned = RowLayoutSpan.Column;
oRowLayout1.Col = 2;
oRowLayout1.Row = 2;
oRowLayouts1.Add(oRowLayout1);
//Notes
WebGridColumnSet oColumnSet2 = new WebGridColumnSet();
oColumnSet2.Caption = "Notes";
oColumnSet2.Name = "ColumnSet2";
oColumnSet2.ColumnCount = 1;WebGrid1.RootTable.ColumnSets.Add(oColumnSet2);
WebGridRowLayoutCollection oRowLayouts2 = oColumnSet2.Layout;
WebGridRowLayout oRowLayout2;
oRowLayout2 = new WebGridRowLayout();
oRowLayout2.Col = 0;
oRowLayout2.Row = 0;
oRowLayout2.RowSpan = 3;
oRowLayout2.ColumnMember = "Notes";
oRowLayouts2.Add(oRowLayout2);oRowLayout2 = new WebGridRowLayout();
oRowLayout2.Spanned = RowLayoutSpan.Column;
oRowLayout2.Col = 0;
oRowLayout2.Row = 1;
oRowLayouts2.Add(oRowLayout2);oRowLayout2 = new WebGridRowLayout();
oRowLayout2.Spanned = RowLayoutSpan.Column;
oRowLayout2.Col = 0;
oRowLayout2.Row = 2;
oRowLayouts2.Add(oRowLayout2);oRowLayout2 = new WebGridRowLayout();
oRowLayout2.Spanned = RowLayoutSpan.Column;
oRowLayout2.Col = 0;
oRowLayout2.Row = 3;
oRowLayouts2.Add(oRowLayout2);
- Finally, compile the project by pressing Ctrl+Shift+B and run the WebForm. The WebGrid will look like the following snapshot:
Tasks
Walkthrough: Creating ColumnSet layout in WebGrid
Walkthrough: Configuring multiline row in ColumnSet layout
How-to: Display a ColumnSet layout hierarchically
How-to: Create ColumnSet layout when IList object is bound to WebGrid
Concepts
ColumnSets Layout
References
ColumnSetSettings Class
WebGridColumnSet Class
Other Resources
Walkthrough Topics
How-to Topics