PC Review


Reply
Thread Tools Rate Thread

Autofit Grid Columns

 
 
Sean
Guest
Posts: n/a
 
      8th Dec 2003
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
 
Reply With Quote
 
 
 
 
Jan Tielens
Guest
Posts: n/a
 
      8th Dec 2003
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" <(E-Mail Removed)> schreef in bericht
news:021f01c3bdbd$367965a0$(E-Mail Removed)...
> 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



 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      8th Dec 2003
* "Sean" <(E-Mail Removed)> 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>
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Rows().AutoFit starts a calculation, but Columns().AutoFit doesn't Stefano Microsoft Excel Programming 1 30th Nov 2009 05:01 PM
RE: Rows().AutoFit starts a calculation, but Columns().AutoFit doesn't Mike H Microsoft Excel Programming 0 25th Nov 2009 04:31 PM
Columns - autofit learning_codes@hotmail.com Microsoft Excel Discussion 5 11th Sep 2009 07:56 PM
RE: Autofit (Columns.EntireColumn.AutoFit) does not work Kevin B Microsoft Excel Misc 0 10th Feb 2009 05:21 PM
RE: Autofit (Columns.EntireColumn.AutoFit) does not work Kevin B Microsoft Excel Misc 0 10th Feb 2009 05:14 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:48 AM.