473,395 Members | 1,527 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,395 software developers and data experts.

Autofit Grid Columns

Hello, I have created a Datagrid which is populated by my
program. I would like to add code to cause each column
to expand to fit its largest item. This is similar to
the autofit in Excel. Is there a good way to do this?

Thanks
Sean
Nov 20 '05 #1
2 3179
One way to do this is to use MeasureString to compute the size of the text
in each cell, and then take the maximum value. Below is a code snippet that
does this. It assumes your datagrid is bound to a datatable.

public void AutoSizeCol(int col)

{

float width = 0;

int numRows = ((DataTable) dataGrid1.DataSource).Rows.Count;

Graphics g = Graphics.FromHwnd(dataGrid1.Handle);

StringFormat sf = new StringFormat(StringFormat.GenericTypographic);

SizeF size;

for(int i = 0; i < numRows; ++ i)

{

size = g.MeasureString(dataGrid1[i, col].ToString(),
dataGrid1.Font, 500, sf);

if(size.Width > width)

width = size.Width;

}

g.Dispose();

dataGrid1.TableStyles["customers"].GridColumnStyles[col].Width = (int)
width + 8; // 8 is for leading and trailing padding

}

http://www.syncfusion.com/FAQ/WinFor...c44c.asp#q877q

--
Greetz,
Jan
__________________________________
Read my weblog: http://weblogs.asp.net/jan
"Sean" <an*******@discussions.microsoft.com> schreef in bericht
news:02****************************@phx.gbl...
Hello, I have created a Datagrid which is populated by my
program. I would like to add code to cause each column
to expand to fit its largest item. This is similar to
the autofit in Excel. Is there a good way to do this?

Thanks
Sean

Nov 20 '05 #2
* "Sean" <an*******@discussions.microsoft.com> scripsit:
Hello, I have created a Datagrid which is populated by my
program. I would like to add code to cause each column
to expand to fit its largest item. This is similar to
the autofit in Excel. Is there a good way to do this?


<http://www.syncfusion.com/faq/winforms/search/1004.asp>

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Girish | last post by:
Im trying to create a grid within a grid programmatically. Ive been successful in doing this but I need the embedded grid to fire its ItemDataBound event so I can handle it. The event does not seem...
3
by: alex | last post by:
Hi, is there an easy way to 1: make the column witdh to fit the text 2: to fit the table into the grid if it is smaller e.g. to add some space space to each column to fit it nicely into the...
4
by: Sean | last post by:
Hello, I would like to use the vb equivalent of the excel autofit in a datagrid. I need each column to be the size of its largest element. I do not want all the columns to be the same size. Any...
1
by: Sean | last post by:
Hello, I have a vb application in which I populate a datagrid from a database. The grid populates fine and the data is correct. I need to autofit the columns so that the width of each column is...
117
by: phil-news-nospam | last post by:
Is there really any advantage to using DIV elements with float style properies, vs. the old method of TABLE and TR and TD? I'm finding that by using DIV, it still involves the same number of...
1
by: myname | last post by:
Hello, I assign a Datatable to a Datagrid.DataSource. All the columns are displayed with the same width. I would like them to be set automatically to the best width (something like Autofit...
0
by: Gian Paolo | last post by:
this is something really i can't find a reason. I have a form with a tabcontrol with tree pages, in the second page there is a Data GRid View. Plus i have a class. When i open the form i...
2
by: Arash | last post by:
Is it possible to autofit column width in Access using vba? Something like the following in Excel: Worksheets("Sheet1").Columns("A:I").AutoFit Thanks in advance,
2
by: Lance Hoffmeyer | last post by:
Hey all, Wondering why the syntax won't autofit Column A? I am not getting any errors. Also, is there a way of reducing the number of syntax lines? Basically, I am wondering if there is an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.