Microsoft KB Archive/317859

= Description of setting column properties in a DataGrid control =

Article ID: 317859

Article Last Modified on 5/11/2007

-

APPLIES TO


 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * 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:  Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. 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:

http://msdn2.microsoft.com/en-us/library/ms379584(vs.80).aspx

For more information about partial classes and the Windows Forms Designer, visit the following MSDN Web site:

http://msdn2.microsoft.com/en-us/library/ms171843.aspx

 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. 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. On the IDE's Data menu, click Generate Dataset. In the Generate DataSet dialog box, click OK to accept the following default settings:  A new dataset that is named Dataset1.</li> The selected item is authors (SQLDataAdapter1).</li> The 'Add this Dataset to the Designer' property has been checked.</li></ul>

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.</li> Select the DataGrid control from the ToolBox, and then drag it on to Form1. By default, this is named DataGrid1.</li> From the Property pane of DataGrid1, set the DataSource property to DataSet11, and then set the DataMember property to authors.</li> From the Property pane of DataGrid1, select TableStyles, and then click Collection.</li> 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.</li> From the Property pane of DataGridTableStyle1, select GridColumnStyles, and then click Collection.</li> 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.</li> Click Add two more times so that you have total of three items in the collection.</li> Set the following properties for DataGridTextboxColumns:

</li> Open the code window for Form1.vb.

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

The form displays a DataGrid control that contains three columns of data.</li></ol>

Keywords: kbvs2005swept kbvs2005applies kbinfo KB317859

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.