DataGrid Component, Resizing Columns Issue(s)

  • 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
*-----------------------*
 
i always use this to pre-set the col-width:


Private Sub SetDefaultColWidth(ByVal defColWidth As Integer)
DataGrid1.PreferredColumnWidth = defColWidth
End Sub

then just call the sub procedure with the argument (size) you want, but i've been doing it on the form load.

SetDefaultColWidth(65)

it's very little code, and i like that
thanks
rik



**********************************************************************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
 
Back
Top