PC Review


Reply
Thread Tools Rate Thread

Datagrid 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 easy way to do this?

 
Reply With Quote
 
 
 
 
Jan Tielens
Guest
Posts: n/a
 
      9th 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

}


SOURCE:
http://www.syncfusion.com/FAQ/WinFor...c44c.asp#q877q
You can download complete examples here too!

--
Greetz

Jan Tielens
________________________________
Read my weblog: http://weblogs.asp.net/jan


"Sean" <(E-Mail Removed)> wrote in message
news:f73501c3bdd8$5de8f680$(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 easy way to do this?
>



 
Reply With Quote
 
Cor
Guest
Posts: n/a
 
      9th Dec 2003
Hi Sean,

I think a combination from something like this

AvgCharWidth =
CInt(Graphics.FromHwnd(Me.Handle).MeasureString("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
, Me.Font).Width / 26.0!)
tsl.GridColumnStyles.Item(0).Width = AvgCharWidth * MaxFoundedItemWidth

And something as this here for this message written (and never tested and
see it as a kind of pseudo)
\\\
dim mycolumnmax as array(numberofcolumns)
For i As Integer = 0 To MyTable.Columns.Count - 1
Dim max As Integer
max = 0
For y As Integer = 0 To MyTable.Rows.Count - 1
If MyTable.Rows(y).Item(i).ToString.Length > max Then
max = MyTable(0).Rows(y).Item(i).ToString.Length
End If
Next
mycolumnmax(i) = max
Next
///

Will maybe do what you ask, I am curious about that?

You have to do it everytime a user enters some data in your grid.

Cor


 
Reply With Quote
 
Cor
Guest
Posts: n/a
 
      9th Dec 2003
Hi Sean,

While doing this, I think suddenly, that it should be able to avoid that
most innerloop from the rows, by using a datatable.compute methode.

That is not one of my favorites, so when you want to use it you would have
to figured out yourself or have the luck that Jay B looks to it and does it
for you.

Cor



 
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
Datagrid, binded columns and unbinded columns =?Utf-8?B?S2VycnkgTW9vcm1hbg==?= Microsoft VB .NET 4 28th Apr 2006 06:02 AM
How to add a Dropdown list to a datagrid at runtime (dynamic) without using template columns in ASP.NET and still have the ability to us the datagrid Update event. Daniel Roth Microsoft Dot NET Framework 0 5th Apr 2005 04:54 AM
dataGrid with columns (more than 1 el. in a row) =?Utf-8?B?TWljaGFsIEphbnVzemN6eWs=?= Microsoft ASP .NET 3 25th Feb 2004 03:16 PM
DataGrid Component | Displaying columns of a DataSet as Rows in a DataGrid ? Diego TERCERO Microsoft C# .NET 3 19th Dec 2003 02:21 PM
datagrid columns scorpion53061 Microsoft VB .NET 2 9th Dec 2003 01:01 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:58 AM.