Grid bound columns in Windows Forms xp toolbar

27 Apr 20216 minutes to read

To control properties of a column in your Grid DataBound Grid, you must use GridBoundColumn class object. You can also explicitly add GridBoundColumn object to GridDataBoundGrid.GridBoundColumns collection for each column that you want to see in the grid or you can let GridDataBoundGrid.Binder class generate these columns for you.

Here are the code samples that will explicitly add GridBoundColumns. Note that you can add these GridBoundColumns at design-time provided you properly set MappingName property for each column.

private void Form1_Load(object sender, System.EventArgs e)
{
    this.gridDataBoundGrid1.DataSource = ReturnATable();
    this.gridDataBoundGrid1.EnableAddNew = false;
    this.gridDataBoundGrid1.BackColor = Color.FromArgb(0xcc, 0xd4, 0xe6);

//Creates GridBoundColumn for each displayed column.
    GridBoundColumn gridBoundColumn = new GridBoundColumn();
    gridBoundColumn.MappingName = "FirstName";  

//Must set to column mapping name.
    gridBoundColumn.HeaderText = "Name";

//Sets some style properties.
    gridBoundColumn.StyleInfo.BackColor = Color.FromArgb(0xC0, 0xC9, 0xdb);
    gridBoundColumn.StyleInfo.TextColor = Color.Blue;

//Adds the column to GridBoundColumns collection.
    this.gridDataBoundGrid1.GridBoundColumns.Add(gridBoundColumn);

//Repeats for each column.
    gridBoundColumn = new GridBoundColumn();
    gridBoundColumn.MappingName = "LastName";
    gridBoundColumn.HeaderText = "FamilyName";
    gridBoundColumn.StyleInfo.Font.Bold = true;
    this.gridDataBoundGrid1.GridBoundColumns.Add(gridBoundColumn);

    gridBoundColumn = new GridBoundColumn();
    gridBoundColumn.MappingName = "City";
    gridBoundColumn.HeaderText = "City";
    this.gridDataBoundGrid1.GridBoundColumns.Add(gridBoundColumn);

//Needs to initialize GridBoundColumns so that their settings will replace currently set values.
    this.gridDataBoundGrid1.Binder.InitializeColumns();

//Resizes the column headers.
    this.gridDataBoundGrid1.Model.ColWidths.ResizeToFit(GridRangeInfo.Row(0), GridResizeToFitOptions.NoShrinkSize);
}
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Me.gridDataBoundGrid1.DataSource = ReturnATable()
Me.gridDataBoundGrid1.EnableAddNew = False
Me.gridDataBoundGrid1.BackColor = Color.FromArgb(&HCC, &HD4, &HE6)

'Creates GridBoundColumn for each displayed column.
Dim gridBoundColumn As New GridBoundColumn()
gridBoundColumn.MappingName = "FirstName"

'Must set to column mapping name.
gridBoundColumn.HeaderText = "Name"

'Sets some style properties.
gridBoundColumn.StyleInfo.BackColor = Color.FromArgb(&HC0, &HC9, &HDB)
gridBoundColumn.StyleInfo.TextColor = Color.Blue

'Adds column to GridBoundColumns collection.
Me.gridDataBoundGrid1.GridBoundColumns.Add(gridBoundColumn)

'Repeats for each column.
gridBoundColumn = New GridBoundColumn()
gridBoundColumn.MappingName = "LastName"
gridBoundColumn.HeaderText = "FamilyName"
gridBoundColumn.StyleInfo.Font.Bold = True
Me.gridDataBoundGrid1.GridBoundColumns.Add(gridBoundColumn)

gridBoundColumn = New GridBoundColumn()
gridBoundColumn.MappingName = "City"
gridBoundColumn.HeaderText = "City"
Me.gridDataBoundGrid1.GridBoundColumns.Add(gridBoundColumn)

'Needs to initialize GridBoundColumns so their settings will replace currently set values.
Me.gridDataBoundGrid1.Binder.InitializeColumns()

'Resizes column headers.
Me.gridDataBoundGrid1.Model.ColWidths.ResizeToFit(GridRangeInfo.Row(0), GridResizeToFitOptions.NoShrinkSize)

'Form1_Load
End Sub

Using the GridDataBoundGrid.Binder Class

Instead of adding your own GridBoundColumns, you can directly reference internal columns that are generated by GridDataBoundGrid.Binder class when data source is set in the grid. You can also use these internal columns to set the style info for columns or HeaderText or any other property of the GridBoundColumn. Here is a Form_Load handler that will display the same grid as above using internal columns.

private void Form1_Load(object sender, System.EventArgs e)
{
    this.gridDataBoundGrid1.DataSource = ReturnATable();
    this.gridDataBoundGrid1.EnableAddNew = false;
    this.gridDataBoundGrid1.BackColor = Color.FromArgb(0xcc, 0xd4, 0xe6);

//Sets the first column.
    GridModelDataBinder binder = this.gridDataBoundGrid1.Binder;
    GridBoundColumn gridBoundColumn = binder.InternalColumns["FirstName"];
    gridBoundColumn.HeaderText = "Name";
    gridBoundColumn.StyleInfo.BackColor = Color.FromArgb(0xC0, 0xC9, 0xdb);
    gridBoundColumn.StyleInfo.TextColor = Color.Blue;

//Sets the second column.
    gridBoundColumn = binder.InternalColumns["LastName"];
    gridBoundColumn.HeaderText = "FamilyName";
    gridBoundColumn.StyleInfo.Font.Bold = true;

//Just uses the default third column... no changes.

//Resizes column headers.
    this.gridDataBoundGrid1.Model.ColWidths.ResizeToFit(GridRangeInfo.Row(0), GridResizeToFitOptions.NoShrinkSize);
}
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Me.gridDataBoundGrid1.DataSource = ReturnATable()
Me.gridDataBoundGrid1.EnableAddNew = False
Me.gridDataBoundGrid1.BackColor = Color.FromArgb(&HCC, &HD4, &HE6)

'Sets the first column.
Dim binder As GridModelDataBinder = Me.gridDataBoundGrid1.Binder
Dim gridBoundColumn As GridBoundColumn = binder.InternalColumns("FirstName")
gridBoundColumn.HeaderText = "Name"
gridBoundColumn.StyleInfo.BackColor = Color.FromArgb(&HC0, &HC9, &HDB)
gridBoundColumn.StyleInfo.TextColor = Color.Blue

'Sets the second column.
gridBoundColumn = binder.InternalColumns("LastName")
gridBoundColumn.HeaderText = "FamilyName"
gridBoundColumn.StyleInfo.Font.Bold = True

'Just uses the default third column... no changes.

'Resizes the column headers.
Me.gridDataBoundGrid1.Model.ColWidths.ResizeToFit(GridRangeInfo.Row(0), GridResizeToFitOptions.NoShrinkSize)

' Form1_Load
End Sub