ARTICLE

Adding Tables and Columns to the Windows Forms DataGrid Control

Posted by Dinesh Beniwal Articles | Visual Basic 2010 September 09, 2009
In this article you will learn how to use Adding Tables and Columns to the Windows Forms DataGrid Control
 
Reader Level:

Adding Tables and Columns to the Windows Forms DataGrid Control
 
You can display data in the Windows Forms DataGrid control in tables and columns by creating DataGridTableStyle objects and adding them to the GridTableStylesCollection object, which is accessed through the DataGrid control's TableStyles property. Each table style displays the contents of whatever data table is specified in the DataGridTableStyle object's MappingName property. By default, a table style with no column styles specified will display all the columns within that data table. You can restrict which columns from the table appear by adding DataGridColumnStyle objects to the GridColumnStylesCollection object, which is accessed through the GridColumnStyles property of each DataGridTableStyle object.

To add a table to the DataGrid control in the designer

  1. In order to display data in the table, you must first bind the DataGrid control to a dataset. For more information, see Binding the Windows Forms DataGrid Control to a Data Source.
  2. Select the DataGrid control's TableStyles property in the Properties window, and then click the ellipsis button () next to the property to display the DataGridTableStyle Collection Editor.
  3. In the collection editor, click the Add button to insert a table style.
  4. Click OK to close the collection editor, then re-open it by clicking the ellipsis button next to the TableStyles property.

    When you reopen the collection editor, any data tables bound to the control will appear in the drop-down list for the MappingName property of the table style.

  5. In the Members box of the collection editor, click the table style.
  6. In the Properties box of the collection editor, select the MappingName value for the table you wish to display.

To add a column to the DataGrid control in the designer

  1. In the Members box of the DataGridTableStyle Collection Editor, select the appropriate table style. In the Properties box of the collection editor, select the GridColumnStyles collection, and then click the ellipsis button () next to the property to display the DataGridColumnStyle Collection Editor.
  2. In the collection editor, click the Add button to insert a column style or click the down arrow next to the Add button to specify a column type. The drop-down box will allow you to select either the DataGridTextBoxColumn or DataGridBoolColumn type.
  3. Click OK to close the DataGridColumnStyle Collection Editor, then re-open it by clicking the ellipsis button next to the GridColumnStyles property.

    When you reopen the collection editor, any data columns in the bound data table will appear in the drop-down list for the MappingName property of the column style.

  4. In the Members box of the collection editor, click the column style.
  5. In the Properties box of the collection editor, select the MappingName value for the column you wish to display.

To add a table and column to a DataGrid programmatically

Caution   When programmatically specifying column styles, always create DataGridColumnStyle objects and add them to the GridColumnStylesCollection object before adding DataGridTableStyle objects to the GridTableStylesCollection object. When you add an empty DataGridTableStyle object to the collection, DataGridColumnStyle objects are automatically generated for you. Consequently, an exception will be thrown if you try to add new DataGridColumnStyle objects with duplicate MappingName values to the GridColumnStylesCollection object.
  1. In order to display data in the table, you must first bind the DataGrid control to a dataset. For more information, see Binding the Windows Forms DataGrid Control to a Data Source.
  2. Declare a new table style and set its mapping name.

            Dim ts1 As New DataGridTableStyle()
            ts1.MappingName = "Customers"
     

  3. Declare a new column style and set its mapping name and other properties.

            Dim myDataCol As New DataGridBoolColumn()
            myDataCol.HeaderText = "My New Column"
            myDataCol.MappingName = "Current"
     

  4. Call the Add method of the GridColumnStylesCollection object to add the column to the table style

            ts1.GridColumnStyles.Add(myDataCol)
     
  5. Call the Add method of the GridTableStylesCollection object to add the table style to the data grid.

            DataGrid1.TableStyles.Add(ts1)

     
     

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Team Foundation Server Hosting
Become a Sponsor