[ Team LiB ] |
Recipe 7.10 Creating Custom Columns in a Windows Forms DataGridProblemYou need to create a DataGrid having columns with custom formatting and attributes. SolutionUse the DataGridTableStyle class. The sample code contains two event handlers:
The C# code is shown in Example 7-18. Example 7-18. File: CustomColumnsInDataGridForm.cs// Namespaces, variables, and constants using System; using System.Configuration; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; private SqlDataAdapter da; private DataTable dt; // . . . private void CustomColumnsInDataGridForm_Load(object sender, System.EventArgs e) { // Create the DataAdapter. String selectCommand = "SELECT ProductID, ProductName, Discontinued " + "FROM Products"; da = new SqlDataAdapter(selectCommand, ConfigurationSettings.AppSettings["Sql_ConnectString"]); // Use CommandBuilder to handle updates back to the data source. SqlCommandBuilder cb = new SqlCommandBuilder(da); // Retrieve the table schema. dt = new DataTable("Products"); da.FillSchema(dt, SchemaType.Source); // Default the check box column to false. dt.Columns["Discontinued"].DefaultValue = false; // Fill the table. da.Fill(dt); // Define the table for the grid. DataGridTableStyle ts = new DataGridTableStyle( ); ts.MappingName = "Products"; // Define and add the columns to the grid. DataGridTextBoxColumn productIDCol = new DataGridTextBoxColumn( ); productIDCol.MappingName = "ProductID"; productIDCol.HeaderText = "Product ID"; productIDCol.ReadOnly = true; ts.GridColumnStyles.Add(productIDCol); DataGridTextBoxColumn productNameCol = new DataGridTextBoxColumn( ); productNameCol.MappingName = "ProductName"; productNameCol.HeaderText = "Name"; productNameCol.Width = 200; productNameCol.Alignment = HorizontalAlignment.Center; ts.GridColumnStyles.Add(productNameCol); DataGridBoolColumn discontinuedCol = new DataGridBoolColumn( ); discontinuedCol.MappingName = "Discontinued"; discontinuedCol.HeaderText = "Discontinued"; discontinuedCol.AllowNull = false; ts.GridColumnStyles.Add(discontinuedCol); dataGrid.TableStyles.Add(ts); // Bind the default view of the table to the grid. dataGrid.DataSource = dt.DefaultView; } private void updateButton_Click(object sender, System.EventArgs e) { // Update the data using the DataAdapter. da.Update(dt); } DiscussionThe TableStyles property of the DataGrid exposes a collection of DataGridTableStyle objects. The GridColumnStyles property of the DataGridTableStyle object exposes a GridColumnStylesCollection containing all DataGridColumnStyle objects for the table. This property allows a custom set of column styles to be created for the DataGrid. Once defined, the Add( ) method of the DataGridTableStyles object is used to add the custom column styles to the DataGrid. The MappingName property of the DataGridTableStyle is set to the DataSource. The MappingName of each DataGridColumnStyle object must be associated with the name of a DataColumn to synchronize the DataGrid display column with the data column. An exception will be thrown if duplicate mapping names are used. The DataGridTextBoxColumn class inherits from the abstract DataGridColumnStyle class. It defines the attributes, display format, and behavior of cells in a DataGrid column. At runtime, each cell in the column hosts a DataGridTextBox control. The DataGridBoolColumn inherits from the abstract DataGridColumnStyle class. It defines the attributes, display format, and behavior of cells in a DataGrid column representing a Boolean value. At runtime, each cell in the column hosts a CheckBox control that can have one of three states: checked (true), unchecked (false), or unchecked (DBNull.Value). The allowable states of the check box are controlled by the properties described in Table 7-10.
A new column class can be created to meet special requirements by inheriting from DataGridColumnStyle, where the Abort( ), Commit( ), Edit( ), and Paint( ) methods must be overridden. By default, a collection of DataGridColumnStyle objects is created behind the scenes when the DataSource property of the DataGrid is set. The class used for each column depends on the DataType of the DataColumn associated with the DataGridColumnStyle object. A column with a Boolean data type will be represented by a DataGridBoolColumn object while other columns will be represented by a DataGridTextBoxColumn object. You can use the GetType( ) method of the DataGridColumnStyle object to determine the column's data type. |
[ Team LiB ] |