kiran
kiran

Reputation: 13

Adjusting datagrid columns in a C# Windows application

How can I adjust the column widths based on a datagrid's displayed values in a C# Windows application?

Upvotes: 0

Views: 1462

Answers (3)

pavanred
pavanred

Reputation: 13793

You can do this by using MeasureString to compute the size of the text in each cell, and then take the maximum value.

You can find the code snippet to do this here - http://www.syncfusion.com/FAQ/windowsforms/faq_c44c.aspx#q877q

Upvotes: 2

Nikos Steiakakis
Nikos Steiakakis

Reputation: 5745

Well one approach is to set the attribute AutoSizeColumnsMode to "Fill" which will resize the columns dynamically in order to fill the extent of the grid. Then, for each column you can also specify the FillWeight (in "Edit Columns") in order to have a "weighted" resizing of the columns.

Otherwise you can set the default width for each column by setting the "Width" attribute in "Edit Columns"

If you want to do this programmatically at runtime, you can do it by calling

dataGridView1.Columns[...].Width = XX

Upvotes: 0

Beatles1692
Beatles1692

Reputation: 5320

If you are using DataGridView as your datagrid control there's an AutoSizeColumns property that if set to true it will adjust columns widths automatically.

Upvotes: 1

Related Questions