How to programmatically set the DataGrid column width to the longest field by using Visual C# 2005 or Visual C# .NET (812422)
The information in this article applies to:
- Microsoft Visual C# 2005, Express Edition
- Microsoft Visual C# .NET (2002)
For a Microsoft Visual Basic .NET version of this
article, see
811203. SUMMARYThis 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 by double-clicking the column
separator. Both methods are independent of the font that is used in the DataGrid column fields. Create a Test Windows Application Project- Start Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET.
- On the File menu, point to
New, and then click Project.
- Under Project Types, click Visual
C# Projects, and then click Windows Application under
Templates.
Note In Visual Studio 2005, click Visual
C# instead of Visual
C# Projects. - Name the project
SampleDataGridColumnWidth, and then click OK.
Data Adapter Configuration WizardTo configure an OleDbDataAdapter control, follow these steps:
- Drag an OleDbDataAdapter control from the toolbox to the form.
Note In Visual Studio 2005, right-click the Toolbox, and then click Choose Items. On the .NET Framework Components tab in the dialog box, select OleDbDataAdapter. Note If you use the SqlDataAdapter control instead of the OleDbDataAdapter control, 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 SQL Server Northwind database. To do this, follow
these steps:
- On the Provider tab, click to select
Microsoft JET 4.0 OLE DB Provider.
- On the Connection tab, under
Select or enter a database name, click
Ellipsis.
- Locate the database Northwind, click Open, and then click
OK.
- In the Data Adapter Configuration Wizard, click
Next.
- Click to select Use SQL Statement, and
then click Next.
- Use the Query Builder to insert the following SQL
statement: To do this, follow these steps:
- Click Query Builder.
- In the Add Table dialog box, click to
select Employees.
- 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.)
- In Employees table, click to select
All Columns.
- To verify the connection, on the Table column,
right-click the connection, and then click Run. In the Results
pane, Employee table data is displayed.
- Click OK.
- Click Finish. Notice that the OleDbConnection1 control is automatically inserted in the project.
Generate a DataSetGenerate a DataSet that is related to OleDbDataAdapter1. To do
this, follow these steps:
- Right-click OleDbDataAdapter1, and then
click Properties.
- In the Properties window, click
Generate Dataset.
- In the Generate Dataset dialog box, click
New, and then name it DataSet1.
- Verify that the Employees table is selected.
- Verify that the option Add this dataset to the
designer is selected, and then click OK.
- In Solution Explorer, right-click
Form1.cs, and then click View
Code.
- To fill the DataSet, add the following code to the Form1
constructor (this is public Form1()):
oleDbDataAdapter1.Fill(dataSet11);
Insert a DataGrid and Bind with a DataSet- Drag a DataGrid from the toolbox to the form.
- In the Properties Window, set the
DataSource property to
DataSet11.Employees.
Implement the Longest Field Search AlgorithmIf the DataGrid is bound to a DataSet, it is faster to search in the DataSet for the longest field, than to seach in the DataGrid column. 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 do this, follow these steps:
- Drag a button control from the toolbox to the form.
- Double-click Button1 and add the following code to Button1_Click event:
// Get the width of Longest Field
int newwidth = LongestField(dataSet11, "Employees", "Title");
// Create new Table Style
DataGridTableStyle ts = new DataGridTableStyle();
ts.MappingName = "Employees";
this.dataGrid1.TableStyles.Clear();
this.dataGrid1.TableStyles.Add(ts);
// Assign New Width to DataGrid column
this.dataGrid1.TableStyles["Employees"].GridColumnStyles["Title"].Width = newwidth;
- Implement the LongestField() function in the Form1 class as follows:
private int LongestField (DataSet ds, string TableName, string ColumnName)
{
int maxlength = 0;
int tot = ds.Tables[TableName].Rows.Count;
string straux = "";
int intaux = 0;
Graphics g = dataGrid1.CreateGraphics();
// Take width one balnk space to add to the new width to the Column
int offset = Convert.ToInt32(Math.Ceiling(g.MeasureString(" ", dataGrid1.Font).Width));
for (int i=0; i<tot; ++i)
{
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)
{
maxlength = intaux;
}
}// End of For Loop
return maxlength + offset;
} - On the Debug menu, click
Start.
- Click Button1. Notice that the Title
column width is modified to the longest-field size.
REFERENCES For additional
information, click the following article number to view the article in the
Microsoft Knowledge Base: 313482
INFO: Roadmap for Windows Forms Data Binding
For additional information, see the following
Microsoft Developer Network (MSDN) Web site:
Modification Type: | Major | Last Reviewed: | 1/9/2006 |
---|
Keywords: | kbHOWTOmaster kbCtrl kbDataBinding kbWindowsForms kbui KB812422 kbAudDeveloper |
---|
|
|
©2004 Microsoft Corporation. All rights reserved.
|
|