Skip to content
Radu Martin edited this page Apr 15, 2017 · 18 revisions

These extenders simplify the work with data-bound in WinForms.

Usage the DataGridView:
gridView.GenerateColumns(DataSource, 
    new ColumnDataDescriptor("Header1", "Column1Name"),
    new ColumnDataDescriptor("Header2", "Column2Name"),
    new ColumnDataDescriptor("Header3", "Column3Name", FillWeight: 100));
Usage the FlowLayoutPanel:
layoutPanel.GenerateGroups(DataSource, 
    new GroupDataDescriptor("GroupName1",
      new FieldDataDescriptor("Header1", "Column1Name"),
      new FieldDataDescriptor("Header2", "Column2Name")),
    new GroupDataDescriptor("GroupName2",
      new FieldDataDescriptor("Header3", "Column3Name")));
Usage the TableLayoutPanel:
layoutPanel.GenerateFields(DataSource, 
    new FieldDataDescriptor("Header1", "Column1Name"),
    new FieldDataDescriptor("Header2", "Column2Name"),
    new FieldDataDescriptor("Header3", "Column3Name"));

Big example

var Database = ...; // create typed DataSet 

// praparing Grids
var tbl1 = this.Database.Departments;
var departmentsBindingSource = new System.Windows.Forms.BindingSource(this.components);
departmentsBindingSource.DataMember = Database.Departments.TableName;
departmentsBindingSource.DataSource = Database;
this.departmentsGridView.GenerateColumns(departmentsBindingSource, 
  new ColumnDataDescriptor("Department", tbl1.DepartmentNameColumn),
  new ColumnDataDescriptor("Is closed?", tbl1.IsClosedColumn),
  new ColumnDataDescriptor("Group", tbl1.CompanyGroupColumn),
  new ColumnDataDescriptor("Remarks", tbl1.RemarksColumn, FillWeight: 100));
this.departmentsGridView.PrepareStyleForEditingData();
this.departmentsGridView.AddDataRowStateDrawingInRowHeaders();

var tbl2 = this.Database.Employees;
var employeesBindingSource = new System.Windows.Forms.BindingSource(this.components);
employeesBindingSource.DataMember = Database.Employees.TableName;
employeesBindingSource.DataSource = Database;
var _salaryGroups = daoDataSet.CreateSalaryGroupsLookupTable();
this.employeesGridView.GenerateColumns(employeesBindingSource,
  new ColumnDataDescriptor("Employee", tbl2.EmployeeNameColumn, FillWeight: 100),
  new ColumnDataDescriptor("Department", tbl2.DepartmentIDColumn, DataSource: tbl1, ValueMember: tbl1.DepartmentIDColumn.ColumnName, DisplayMember: tbl1.DepartmentNameColumn.ColumnName),
  new ColumnDataDescriptor("Phone", tbl2.PhoneNumberColumn, Mode: ColumnEditorMode.TextBox, FormatValueMethod: new FormatValueDelegate(this.FormatPhoneValue)),
  new ColumnDataDescriptor("Date of birth", tbl2.DateBirthColumn, Style: EditorDataStyle.Date),
  new ColumnDataDescriptor("Group of Salary", tbl2.SalaryGroupColumn, DataSource: _salaryGroups, ValueMember: _salaryGroups.Columns[0].ColumnName, DisplayMember: _salaryGroups.Columns[1].ColumnName),
  new ColumnDataDescriptor("Salary", tbl2.SalaryColumn, Style: EditorDataStyle.Money));
this.employeesGridView.PrepareStyleForEditingData();
this.employeesGridView.AddDataRowStateDrawingInRowHeaders();

// preparing Panels
this.departmentsPanel.GenerateGroups(this.toolTip, departmentsBindingSource, 
  new GroupDataDescriptor("Identifications",
    new FieldDataDescriptor("ID", tbl1.DepartmentIDColumn, IsReadOnly: true),
    new FieldDataDescriptor("Department", tbl1.DepartmentNameColumn)),
  new GroupDataDescriptor("Overview", (int)DataDescriptorSizeWidth.Smaller,
    new FieldDataDescriptor("Is closed", tbl1.IsClosedColumn),
    new FieldDataDescriptor("Group", tbl1.CompanyGroupColumn, DataSource: new string[] { "AM", "HR", "MM", "MN", "KT", "SM" })),
  new GroupDataDescriptor("Additions",
    new FieldDataDescriptor("Remarks", tbl1.RemarksColumn)));

this.employeesPanel.GenerateGroups(this.toolTip, employeesBindingSource, 
  new GroupDataDescriptor("Identifications",
    new FieldDataDescriptor("ID", tbl2.EmployeeIDColumn, IsReadOnly: true),
    new FieldDataDescriptor("Employee", tbl2.EmployeeNameColumn),
    new FieldDataDescriptor("Department", tbl2.DepartmentIDColumn, DataSource: tbl1, ValueMember: tbl1.DepartmentIDColumn.ColumnName, DisplayMember: tbl1.DepartmentNameColumn.ColumnName)),
  new GroupDataDescriptor("Overview", (int)DataDescriptorSizeWidth.Small + (int)DataDescriptorSizeWidth.Smaller,
    new FieldDataDescriptor("Phone", tbl2.PhoneNumberColumn, Mode: FieldEditorMode.TextBox, FormatValueMethod: new FormatValueDelegate(this.FormatPhoneValue)),
    new FieldDataDescriptor("Date of birth", tbl2.DateBirthColumn, Style: EditorDataStyle.Date)),
  new GroupDataDescriptor("Salary", (int)DataDescriptorSizeWidth.Small,
    new FieldDataDescriptor("Group", tbl2.SalaryGroupColumn, Mode: FieldEditorMode.ListBox, DataSource: _salaryGroups, ValueMember: _salaryGroups.Columns[0].ColumnName, DisplayMember: _salaryGroups.Columns[1].ColumnName),
    new FieldDataDescriptor("Salary", tbl2.SalaryColumn, Style: EditorDataStyle.Money)));

Preview

Supported .NET Frameworks

DataViewExtenders use special things that were introduced in .NET Framework 4.0:

  • Extension Methods (introduced in .NET Framework 3.5)
  • String.IsNullOrWhiteSpace method
  • Tuple Class