467,868 Members | 1,291 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,868 developers. It's quick & easy.

DataGrid Column Resizing Issues

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[i];
}

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
*-----------------------*
Nov 16 '05 #1
  • viewed: 5078
Share:
3 Replies
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).

--
Scott
http://www.OdeToCode.com/blogs/scott/

On 22 Nov 2004 12:56:12 -0600,
ri**********@hotmail-dot-com.no-spam.invalid (bismarkjoe) wrote:
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[i];
}

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
*-----------------------*


Nov 16 '05 #2
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.***
--------------------
Subject: DataGrid Column Resizing Issues
From: ri**********@hotmail-dot-com.no-spam.invalid (bismarkjoe)
Newsgroups: microsoft.public.dotnet.languages.csharp
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-15
Content-Transfer-Encoding: 8bit
User-Agent: newsSync (.NET Forum (DotNet Forum)) 283034
Message-ID: <41**********@Usenet.com>
Date: 22 Nov 2004 12:56:12 -0600
Lines: 42
X-Authenticated-User: $$f_w91tn_g
X-Comments: This message was posted through <A href
X-Comments2: IMPORTANT: Usenet.com does not condone,
X-Report: Please report illegal or inappropriate use to
Organization: Usenet.com - #1 Newsgroups service on the planet! - http://www.Usenet.com
Path: cpmsftngxa10.phx.gbl!TK2MSFTFEED02.phx.gbl!tornado .fastwebnet.it!tiscali!newsfeed1.ip.tiscali.net!ne ws.glorb.com!news-out2.spamkiller.net!text- east!not-for-mailXref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.languages.csharp:288370
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

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[i];
}

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
*-----------------------*

Nov 16 '05 #3
I have a source code that does automatic column resizing within a datagrid.
Mail me if you're interested.

Thierry.

"bismarkjoe" <ri**********@hotmail-dot-com.no-spam.invalid> a écrit dans le
message de news: 41**********@Usenet.com...
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[i];
}

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
*-----------------------*

Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Paul | last post: by
1 post views Thread by Geroge D. Lake | last post: by
7 posts views Thread by GaryDean | last post: by
7 posts views Thread by Mark B | last post: by
reply views Thread by jack112 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.