473,385 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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
3 5350
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Lalit Bhatia | last post by:
Hi, In Datagrid, while setting width of column through GridColumnStyles collection. width does not set to exact width that I am setting in my code. If I set width to 13, it is changed to 130 or...
2
by: Josef Meile | last post by:
Hi, I'm using a ComboBox, some Textboxes, and a DataGrid to represent a many-to-many relationship between Person and Course. Each time that I change the value in the ComboBox (which for now is...
0
by: Paul | last post by:
Hi I have a datagrid with two different hyperlink columns. The columns open up a new window as the target is set to _blank for both. The windows are different sizes (one large, one small) and I...
1
by: Geroge D. Lake | last post by:
Hi, I need to disable the resizing of a datagrid. I have tried al day and no luck. Any Ideas? Thanks. George.
4
by: skOOb33 | last post by:
I successfully autosized the columns and rows on my Datagrid, and am now facing another issue. Having the sorting ability by clicking the column headers is key, but when I do that, it resizes all...
2
by: Nina | last post by:
Hi there, I've tried everything that I know to prevent usre resizing datagrid columns, but nothing works. Following are the code that I used. Please tell me what's wrong with them. Thank you....
2
by: Mike Baugh | last post by:
I am using visual studio 2005 to develop a form using c# I have 3 datagrids on one form. I can set the row color based on a certain value in a column. However this color applies to all 3...
7
by: GaryDean | last post by:
(one of our developers posted this and it got no answer so I'm giving it another try) I'm converting a DataGrid utility component that previously used the columns array to function as it has in...
7
by: Mark B | last post by:
Can someone write some VB.Net example code for me that does this: 1) Creates a gridview control with the results of a SQL stored procedure that has 1 parameter (text) 2) Adds an extra column...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.