DataGrid Column Resizing Issues

  • Thread starter Thread starter bismarkjoe
  • Start date Start date
B

bismarkjoe

Hello,

I am trying to set the widths on the columns of a DataGrid component,
and I'm not sure if I'm doing it correctly. My code is below:

//load some inital data
table = db.GetDataTable( "SELECT 'Task' AS 'Type', title AS 'Title',
IFNULL(u.name, 'Nobody') AS 'Assigned To' FROM tasks t LEFT JOIN
users u ON t.assigned_to=u.user_id LIMIT 0, 30" );
grid.ReadOnly = true;

//add the table style
DataGridTableStyle TableStyle = new DataGridTableStyle(true);
grid.TableStyles.Add( TableStyle );

//set the datasource view
grid.DataSource = table.DefaultView;
int[] widths = { 35, 175, 70 };
for( int i = 0; i < table.Columns.Count; i++ )
{
if( i < TableStyle.GridColumnStyles.Count )
TableStyle.GridColumnStyles.Width = widths;
}



This seems to work for me initially. However it seems like a lot of
code to just set the widths of columns, so I was wondering if there
is an easier/better way?

Also, on a side note if I give the table a name (i.e. table.TableName
="Tasks";) before I attempt to resize the columns with the code
above. Then it will not resize the columns at all (The
TableyStyle.GridColumnStyles.Count is 0)... why is this?


Thanks in advance.. this problem has been frustrating me for a while.

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
 
Hi bismarkjoe:

There is a private method on the DataGrid with the name of
ColAutoResize. If you use a decompiler / disassembler you'll see MS
has pretty much the same code (except for only one column at a time -
because this method is invoked when a user double clicks on the column
resizer).
 
Hi there,

The easiest and fastest way to do this would be in design time. You can do this via the Property Builder.

Hope that helps,

Michelle Hlaing

Microsoft Support Professional

***Disclaimer: This posting is provided "as is" with no warranties and confers no rights.***
--------------------
 
I have a source code that does automatic column resizing within a datagrid.
Mail me if you're interested.

Thierry.
 
Back
Top