views:

417

answers:

3

hai how can we Adjust the column widths based on the displayed values in a datagrid in c# windows application?.

+1  A: 

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.

Beatles1692
i want to know about datagrid
kiran
this might help http://windowsclient.net/downloads/folders/starterkits/entry1303.aspx
Beatles1692
A: 

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
Nikos Steiakakis
its a datagrid not datagridviewer
kiran
@kiran: If you're creating this from scratch, I highly recommend the `DataGridView` control. `GridView` is deprecated, and offers less functionality.
Codesleuth
ok i changed it to datagridviewer.thanks alot
kiran
i changed it to datagridviewer.thanks alot
kiran
+2  A: 

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

Pavanred
its a sms to PC application so each row is adding to grid one by one.so how can i change the cell width for this?
kiran
you can perhaps have a variable which holds the width of the max cell value for each column. So when each row is added, you can compare the new value with the previous maximum value and set the column width accordingly.
Pavanred