Customizing .NET Windows Form DataGrid's More Information On Installing The .NET Framework Click Here.download Examples from this article.
DataGrid Table Styles
The Windows Form DataGrid control displays data in the form of a grid. The DataGridTableStyle is a class that represents the drawn grid only. This grid should not be confused with the DataTable class, which is a possible source of data for the grid. Instead, the DataGridTableStyle strictly represents the grid as it is painted in the control. Therefore, through the DataGridTableStyle you can control the appearance of the grid for each DataTable. to specify which DataGridTableStyle is used when displaying data from a particular DataTable, set the MappingName to the Tablename of a datatable.
The GridTableStylesCollection contains all the DataGridTableStyle objects used by a System.Windows.Forms.DataGrid control. The collection can contain as many DataGridTableStyle objects as you need, however the MappingName of each must be unique. At run time, this allows you to substitute a DiffERENT DATAGRIDTABLESTYLE for the Same Data, Depending on The User's Preference.
DataGrid Column Styles
The DataGrid control automatically creates a collection of DataGridColumnStyle objects for you when you set the DataSource property to an appropriate data source The objects created actually are instances of one of the following classes that inherit from DataGridColumnStyle:. DataGridBoolColumn or DataGridTextBoxColumn class.
You can also create your own set of DataGridColumnStyle objects and add them to the GridColumnStylesCollection. When you do so, you must set the MappingName of each column style to the ColumnName of a DataColumn to synchronize the display of columns with the actual data.Example
..................... ..
using System; using System.ComponentModel; using System.Data; using System.Drawing; using System.Windows.Forms; using System.Data.SqlClient; public class CustomGridStyles: System.Windows.Forms.Form {private Button btnShowOrders; private DataGrid myDataGrid; private DataSet myDataSet; private Button btnShowCustomers; private bool TablesAlreadyAdded; public CustomGridStyles () {InitializeComponent (); // Get Customers and Orders from the Database MakeDataSet (); // Show the Customers Table withot custom Table Style myDataGrid.DataSource = myDataSet.Tables [ "Customers"];} ........ public static void Main () {Application.Run (new CustomGridStyles ());} // Show Orders Table with custom Table / Column Style protected void btnShowOrders_Click (Object sender, system.eventargs e) {if (! TableSalReadded) {addcustomDataTyle ();} myDataGrid.setdatabinding (MyDataSet, "ORDERS"); MyDataGrid.captionText = "Formatt ed Orders Table ";} // Show Customers Table with custom Table / Column Style private void btnShowCustomers_Click (object sender, System.EventArgs e) {if {AddCustomDataTableStyle () (TablesAlreadyAdded!);} myDataGrid.SetDataBinding (myDataSet," Customers " ); myDataGrid.CaptionText = "Formatted Customers Table";} // Create own Table / Column Styles private void AddCustomDataTableStyle () {// Create the first DataGridTableStyle which is used // for the "Customers" Table DataGridTableStyle ts1 = new DataGridTableStyle ( ); Ts1.mappingname = "customers";
. // Set other properties ts1.AlternatingBackColor = Color.LightBlue; // Create a first column style and set its MappingName // to the "CustomerID" column of the "Customers" Table // Set the HeaderText and Width properties Well.. I know // for the "CustomerID" Column it makes not much sense to // set a DataGridBoolColumn ... it's just a Demo DataGridColumnStyle boolCol = new DataGridBoolColumn ();. boolCol.MappingName = "CustomerID"; boolCol.HeaderText = " ID "; boolCol.Width = 50; ts1.GridColumnStyles.Add (boolCol); // Add a second column style and set its MappingName // to the" CompanyName "column of the" Customers "Table DataGridColumnStyle textCol = new DataGridTextBoxColumn (. ); TextCol.MappingName = "CompanyName"; TextCol.HeaderText = "Company"; TextCol.Width = 250; ts1.GridColumnStyles.Add (textCol); // Create the second DataGridTableStyle which is used // for the "Orders" Table DA taGridTableStyle ts2 = new DataGridTableStyle (); ts2.MappingName = "Orders";. // Set other properties ts2.AlternatingBackColor = Color.LightGray; // Use a PropertyDescriptor to create a formatted // column First get the PropertyDescriptorCollection // for. the data source and data member PropertyDescriptorCollection pcol = this.BindingContext [myDataSet, "Orders"] GetItemProperties ();. // Create a first formatted column style and set its MappingName // to the OrderDate "column of the" Orders "
. Table // Format the DateTime as Long Format "F" DataGridColumnStyle cOrderDate = new DataGridTextBoxColumn (pcol [ "OrderDate"], "F", true); cOrderDate.MappingName = "OrderDate"; cOrderDate.HeaderText = "Formatted Order Date" ; cOrderDate.Width = 200; ts2.GridColumnStyles.Add (cOrderDate);.. // Create a second formatted column using a PropertyDescriptor // The formatting character "c" specifies a currency format DataGridColumnStyle csOrderFreight = new DataGridTextBoxColumn (pcol [ "Freight "]," c ", true); csOrderFreight.MappingName =" Freight "; csOrderFreight.HeaderText =" Formatted Freight "; csOrderFreight.Width = 100; ts2.GridColumnStyles.Add (csOrderFreight); // Add the DataGridTableStyle instances to / / the gridablestylescollection mydatagrid.tablestyles.add (ts1); mydatagrid.tablestyles.add (ts2); // sets the TableSalReadded to True So this Doesn't happen again. TABLESALREAD yAdded = true;.} // Create a DataSet with two tables and populate it private void MakeDataSet () {// Setup DB-Connection string ConnectionString = "data source = xeon; uid = sa; password = manager; database = northwind" ; SqlConnection cn = new SqlConnection (ConnectionString); // Create the DataSet myDataSet = new DataSet ( "CustOrders");. // Fill the Dataset with Customers, map Default Tablename // "Table" to "Customers" SqlDataAdapter da1 = new SqlDataAdapter ("Select * from Customers", CN);