Microsoft KB Archive/811203

= How to programmatically set the DataGrid column width to the longest field by using Visual Basic .NET =

Article ID: 811203

Article Last Modified on 5/10/2007

-

APPLIES TO


 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition

-





For a Microsoft Visual C# .NET version of this article, see 812422.

IN THIS TASK
 * SUMMARY
 * Create a Test Windows Application Project
 * Data Adapter Configuration Wizard
 * Generate a DataSet
 * Insert a DataGrid and Bind It with DataSet
 * Implement the Longest Field Search Algorithm
 * REFERENCES



SUMMARY
This step-by-step article describes how to programmatically set the column width of a DataGrid to the longest field of the DataGrid column. You can also do this if you double-click Column Separator. Both of these methods are independent of the font that is used in the DataGrid column fields.

back to the top

Create a Test Windows Application Project

 * 1) In Visual Studio .NET, point to New on the File menu, and then click Project.
 * 2) Click Visual Basic Projects, and then click Windows Application.
 * 3) Name the project SampleDataGridColumnWidth, and then click OK.

back to the top

=== Data Adapter Configuration Wizard

===

To configure OleDbDataAdapter1, follow these steps:  Drag an OleDbDataAdapter from the toolbox (located under Data) to Form1.

Note If you use SqlDataAdapter instead of the OleDbDataAdapter, follow the analogous steps. In the Data Adapter Configuration Wizard, click Next. Click New Connection. In the Data Link Properties dialog box, select the connection to the Northwind database. To do this, follow these steps:  On the Provider tab, click to select theMicrosoft JET 4.0 OLE DB Provider check box. On the Connection tab, click Ellipsis under Select or enter a database name. Locate the Northwind database, click Open, and then click OK.</li></ol> </li> In the Data Adapter Configuration Wizard, click Next.</li> Click to select Use SQL Statement check box.</li> Click Next.</li> Use the Query Builder to insert the SELECT FROM Employees SQL statement. To do this, follow these steps: <ol style="list-style-type: lower-alpha;"> Click Query Builder.</li> In the Add Table dialog box, click to select the Employees table.</li> Click Add, and then click Close.

If the Add Table dialog box does not appear, right-click the Query Builder dialog box, and then click Add Table.</li> In the Employees table, click to select the All Columns check box.</li> To verify the connection, right-click the connection, and then click Run on the Table column.

In the results pane, Employee table data is displayed.</li> Click OK.</li></ol> </li> Click Finish.

Notice that the OleDbConnection1 control is automatically inserted into the project.</li></ol>

back to the top

Generate a DataSet
To generate a DataSet that is related to oleDbDataAdapter1, follow these steps: <ol> Right-click OleDbDataAdapter1, and then click Properties.</li> In the Properties window, click Generate Dataset.</li> In the Generate Dataset dialog box, click New, and then type the name DataSet1 .</li> Verify that the Employees table is selected.</li> <li>Verify that the Add this DataSet to the designer option is selected.</li> <li>Click OK.</li> <li>In Solution Explorer, right-click Form1.vb, and then click View Code.</li> <li> To fill the DataSet, append the following code to the Form1 constructor. The Form1 constructor is Public Sub New. OleDbDataAdapter1.Fill(DataSet11) </li></ol>

back to the top

Insert a DataGrid and Bind It with DataSet
To insert a DataGrid and bind it with DataSet, follow these steps:
 * 1) Drag a DataGrid from the toolbox (located under Windows Forms) to Form1.
 * 2) In the Properties window, set the DataSource property to DataSet11.Employees.

back to the top

Implement the Longest Field Search Algorithm
To measure the width of the specified string that is drawn with the specified font, use the MeasureString method of the graphics object that is retrieved from the DataGrid. To Search in the DataSet for the longest field is faster than when you search in the DataGrid column if the DataGrid is bound to a DataSet. To search in the DataSet for the longest field, follow these steps: <ol> <li>Drag a button from the toolbox (located under Windows Forms) to Form1.</li> <li> Double-click Button1, and then add the following code to the Button1_Click event: ' Get the width of the Longest Field. Dim newwidth As Integer = LongestField(DataSet11, &quot;Employees&quot;, &quot;Title&quot;)

' Create new Table Style. Dim ts As New DataGridTableStyle ts.MappingName = &quot;Employees&quot; DataGrid1.TableStyles.Clear DataGrid1.TableStyles.Add(ts)

' Assign New Width to DataGrid column. DataGrid1.TableStyles(&quot;Employees&quot;).GridColumnStyles(&quot;Title&quot;).Width = newwidth </li> <li> Implement the LongestField function in the Form1 class, as follows: Private Function LongestField(ByVal ds As DataSet, ByVal TableName As String, ByVal ColumnName As String) As Integer

Dim maxlength As Integer = 0 Dim g As Graphics = DataGrid1.CreateGraphics

' Take width of one blank space and add to the new width of the Column. Dim offset As Integer = Convert.ToInt32(Math.Ceiling(g.MeasureString(&quot; &quot;, DataGrid1.Font).Width))

Dim i As Integer = 0 Dim intaux As Integer Dim straux As String Dim tot As Integer = ds.Tables(TableName).Rows.Count

For i = 0 To (tot - 1) straux = ds.Tables(TableName).Rows(i)(ColumnName).ToString ' Get the width of Current Field String according to the Font. intaux = Convert.ToInt32(Math.Ceiling(g.MeasureString(straux, DataGrid1.Font).Width)) If (intaux > maxlength) Then maxlength = intaux End If Next

Return maxlength + offset

End Function </li> <li>On the Debug menu, click Start.</li> <li>Click Button1.

Notice that the Title column width is modified to the longest-field size.</li></ol>

back to the top

<div class="references_section">