Newbie - resizing datagrid columns programatically

M

Mamba

Hi all

Working on a winform app that will read a table structure and display the
column names within a datagrid. I want to adjust the column widths to fit
the control size where possible.

Instead of attacking each column separately, I opted for a "higher level"
fix for column widths, and just tried to set the PreferredColumnWidth based
on the number of columns and a guesstimate of the available pixels in the
control.

ie.
dataGrid2.PreferredColumnWidth = numCols/availPixels;
dataGrid2.Refresh()

It seems that it works once or twice when I select tables with decreasing
numbers of columns - I see the column width expand to fill in the available
space. However, when I select tables with increasing numbers of columns,
the column width seems to "stick" at a higher value and screw up the
display, even though I can see the PreferredColumnWidth property assignment
happening correctly in the debugger.

Anybody know A) why this is happening?, B) how to fix it, or C) a better way
to handle this?

Thanks
Gary
 
C

cypher_key

First it's important to mention that I am also a newbie but I should
attempt to contribute to the group as I am taking from it.

The way that I programatically set the width of the columns in the
datagrid was using a DataGridTableStyle. I got this idea from
http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q792q.

Basically I did:
DataGridTableStyle dgts = new DataGridTableStyle(); // Create a
new table style
dgts.MappingName = _dvSearchResults.Table.TableName; // Map it to
the table in my DataView
dgSearchResults.TableStyles.Add(dgts); // Add it to my DataGrid
dgts.GridColumnStyles[0].Width = 50; // Set the width for the
first column
dgts.GridColumnStyles[1].Width = 100; // Set the width for the second
column
dgts.GridColumnStyles[2].Width = 0; // Hide the third

I hope this helps.

Aaron
 
C

Chris Shepherd

Mamba said:
Hi all

Working on a winform app that will read a table structure and display the
column names within a datagrid. I want to adjust the column widths to fit
the control size where possible.

Instead of attacking each column separately, I opted for a "higher level"
fix for column widths, and just tried to set the PreferredColumnWidth based
on the number of columns and a guesstimate of the available pixels in the
control.

Two things.
1- Why not just use the column AutoSizeMode properties?
2- Can you post a simple but functioning example of this issue? I
suspect there's something to do with the DataGrid's sources not being
cleared when it needs to be, but a [code sample] is worth a thousand
words. :)

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top