Format .NET Winforms Datagrid columns using GridColumnStyles
The easiest way to give columns in a DataGrid the look you want is to use a
DataGridTableStyle and to add a GridColumnStyle
for each column in the grid.
Retrieve the "Customers" data using a DataAdapter and
call the DataAdapter's Fill method to populate the
DataSet. Here's some VB.NET code:
Imports System.Data.OleDb
Dim strSQL As String = "Select * From Customers"
Dim Connection As New OleDbConnection(strConnection)
Dim DA As New OleDbDataAdapter(strSQL, Connection)
Dim DS As New DataSet
DA.Fill(DS, "Customers")
Format the DataGrid by adding a DataGrid Table Style.
'
' Create a Grid Table Style. Map it to the "Customers" Table.
'
Dim aGridTableStyle As New DataGridTableStyle
aGridTableStyle.MappingName = "Customers"
'
' Create GridColumnStyle objects for the grid columns
'
Dim aCol1 As New DataGridTextBoxColumn
Dim aCol2 As New DataGridTextBoxColumn
Dim aCol3 As New DataGridTextBoxColumn
Dim aCol4 As New DataGridTextBoxColumn
'
' Hide column 1 by setting its width to 0.
'
With aCol1
.MappingName = "Customer_ID"
.Width = 0
End With
'
' Set column 2's caption, width and disable editing.
'
With aCol2
.MappingName = "Customer_Name"
.HeaderText = "Customer"
.Width = 65
.Alignment = HorizontalAlignment.Left
.TextBox.Enabled = False
End With
'
' Set column 3 and 4's caption, width and enable editing.
' Since these values are optional set their Null values.
'
With aCol3
.MappingName = "Last_Order_Date"
.HeaderText = "Order Date"
.Width = 50
.Alignment = HorizontalAlignment.Center
.NullText = ""
.TextBox.Enabled = True
.Format = "yyyy-MM-dd"
End With
With aCol4
.MappingName = "Last_Order_Amount"
.HeaderText = "Amount"
.Width = 50
.Alignment = HorizontalAlignment.Right
.NullText = "0"
.TextBox.Enabled = True
.Format = "#0.00"
End With
'
' Add the GridColumnStyles to the DataGrid's Column Styles collection.
' Place the "ID" column (column 1) last since it is not visible.
'
With aGridTableStyle.GridColumnStyles
.Add(aCol2)
.Add(aCol3)
.Add(aCol4)
.Add(aCol1)
End With
'
' Add the GridColumnStyles to the aGridTableStyle.
'
DataGrid.TableStyles.Add(aGridTableStyle)
Bind the DataSet to the DataGrid using the DataGrid's DataSource
property. Expand the DataGrid's treeview and navigate to first row of the
Customers table.
'
' Bind the DataGrid to the DataSet. Expand and navigate to first row.
'
If DS.Tables(0).Rows.Count > 0 Then
With DataGrid
.DataSource = DS.Tables(0)
.Expand(-1)
.NavigateTo(0, "Customers")
End With
End If
For the C# coder:
using System.Data;
using System.Data.OleDb;
string strSQL = "Select * From Customers";
OleDbConnection Connection = new OleDbConnection(strConnection);
OleDbDataAdapter DA = new OleDbDataAdapter(strSQL, Connection);
DataSet DS = new DataSet();
DS = new DataSet();
DA.Fill(DS, "Customers")
//
// Create a Grid Table Style. Map it to the "Customers" Table.
//
DataGridTableStyle aGridTableStyle = new DataGridTableStyle();
aGridTableStyle.MappingName = "Customers";
//
// Create GridColumnStyle objects for the grid columns
//
DataGridTextBoxColumn aCol1 = new DataGridTextBoxColumn();
DataGridTextBoxColumn aCol2 = new DataGridTextBoxColumn();
DataGridTextBoxColumn aCol3 = new DataGridTextBoxColumn();
DataGridTextBoxColumn aCol4 = new DataGridTextBoxColumn();
//
// Hide column 1 by setting its width to 0.
//
aCol1.MappingName = "Customer_ID";
aCol1.Width = 0;
//
// Set column 2's caption, width and disable editing.
//
aCol2.MappingName = "Customer_Name";
aCol2.HeaderText = "Customer";
aCol2.Width = 65;
aCol2.Alignment = HorizontalAlignment.Left;
aCol2.TextBox.Enabled = false;
//
// Set column 3 and 4's caption, width and enable editing.
// Since these values are optional set their Null values.
//
aCol3.MappingName = "Last_Order_Date";
aCol3.HeaderText = "Order Date";
aCol3.Width = 50;
aCol3.Alignment = HorizontalAlignment.Center;
aCol3.NullText = "";
aCol3.TextBox.Enabled = true;
aCol3.Format = "yyyy-MM-dd";
aCol4.MappingName = "Last_Order_Amount";
aCol4.HeaderText = "Amount";
aCol4.Width = 50;
aCol4.Alignment = HorizontalAlignment.Right;
aCol4.NullText = "0";
aCol4.TextBox.Enabled = true;
aCol4.Format = "#0.00";
//
// Add the GridColumnStyles to the DataGrid's Column Styles collection.
// Place the "ID" column (column 1) last since it is not visible.
//
aGridTableStyle.GridColumnStyles.Add(aCol2);
aGridTableStyle.GridColumnStyles.Add(aCol3);
aGridTableStyle.GridColumnStyles.Add(aCol4);
aGridTableStyle.GridColumnStyles.Add(aCol1);
//
// Add the GridColumnStyles to the aGridTableStyle.
//
DataGrid.TableStyles.Add(aGridTableStyle);
//
// Bind the DataGrid to the DataSet. Expand and navigate to first row.
//
if (DS.Tables[0].Rows.Count > 0)
{
DataGrid.DataSource = DS.Tables[0];
DataGrid.Expand(-1);
DataGrid.NavigateTo(0, "Customers");
}
|