Description of setting column properties in a DataGrid control (317859)



The information in this article applies to:

  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic 2005

This article was previously published under Q317859

SUMMARY

When a DataGrid is added to a Windows form, the TableStyles and GridColumnStyles are set to the defaults, and the relevant collections are empty. To customize the column settings of the DataGrid, you must first add items to the corresponding collections.

MORE INFORMATION

To customize the column settings for a DataGrid control:
  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. On the File menu, click New and then click Project. Click Windows Application under Visual Basic Projects. By default, Form1 is created.

    Note The code should be changed in Visual Basic 2005. If you create a new form named Form1 in Visual Basic 2005, a Form1.vb file is created for your code and a Form1.Designer.vb file is created that contains the automatically generated part. The Windows Forms Designer uses the partial keyword to divide the implementation of Form1 into two separate files. This prevents the designer-emitted code from being interspersed with your code.

    For more information about the new Visual Basic 2005 language enhancements, visit the following Microsoft Developer Network (MSDN) Web site: For more information about partial classes and the Windows Forms Designer, visit the following MSDN Web site:
  3. On the Server Explorer tab, add a new connection to the Microsoft SQL Server database by right-clicking on the Data Connection item, and then clicking Add Connection to open the Data Link Properties dialog box. Create a connection to the Pubs sample database from this dialog box.
  4. Expand the folder that contains the connection you created, and the Tables subfolder. From the Tables folder, drag the authors table to Form1 to add the new SQLConnection that is named sqlConnection1 and and SQLDataAdapter that is named sqlDataAdapter1 to the Component Tray of the form.
  5. On the IDE's Data menu, click Generate Dataset.
  6. In the Generate DataSet dialog box, click OK to accept the following default settings:
    • A new dataset that is named Dataset1.
    • The selected item is authors (SQLDataAdapter1).
    • The 'Add this Dataset to the Designer' property has been checked.
    Note that the name of the DataSet is DataSet1, but the name that is used to identify this DataSet in code is DataSet11. The DataSet11 is the name that appears and identifies this DataSet in the component tray.
  7. Select the DataGrid control from the ToolBox, and then drag it on to Form1. By default, this is named DataGrid1.
  8. From the Property pane of DataGrid1, set the DataSource property to DataSet11, and then set the DataMember property to authors.
  9. From the Property pane of DataGrid1, select TableStyles, and then click Collection.
  10. In the DataGridTableStyle Collection Editor, click Add to add the first element, DataGridTableStyle1. To the MappingName property of this element, type the name of the table from the DataSet, in this case, authors. Note that this is an important step. If this value is left empty, any custom settings will be ignored, and the default settings will continue to be displayed.
  11. From the Property pane of DataGridTableStyle1, select GridColumnStyles, and then click Collection.
  12. In the DataGridColumnStyle Collection Editor, click Add to add the first element, DataGridTextBoxColumn1.

    Note that the Add drop-down list box has two types of columns; the default of DataGridTextboxColumn, and DataGridBoolColumn.
  13. Click Add two more times so that you have total of three items in the collection.
  14. Set the following properties for DataGridTextboxColumns:

    MemberMappingNameHeaderTextWidth
    DataGridTextBoxColumn1au_idID72
    DataGridTextBoxColumn2au_fnameFirst Name72
    DataGridTextBoxColumn3au_lnameLast Name72
  15. Open the code window for Form1.vb.

    Note In Visual Studio 2005, Form1.vb should be changed to Form1.Designer.vb.
  16. Expand the Region, Windows Form Designer-generated code.
  17. In Sub New() after the "Add any initialization after the InitializeComponent() call" comment, add the following code:
       SqlDataAdapter1.Fill(DataSet11)
    						
    This fills the DataSet11 with the information that was queried from the Pubs table.
  18. Save your work, and then press F5 to run your application.

    The form displays a DataGrid control that contains three columns of data.

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005swept kbvs2005applies kbinfo KB317859 kbAudDeveloper