Link to home
Start Free TrialLog in
Avatar of connelvalentine
connelvalentine

asked on

VB.NET - Dynamically adjusting column widths in datagrid

Hi,
I'm creating an application in VB.NET. I've created a datagrid to display records from my database.

This datagrid will actually be showing data from different tables, depending on table the user wishes to view, so i'm creating and configuring the data adapter and dataset via code and then refreshing the datagrid.

I have a function which runs every time a user selects to display a table on the datagrid. It's a simple function that dynamically creates the datagridtextcolumns based on the number of columns in the table in the dataset. So far, the program works well.

However, the only part i'm having a problem with is the column width. I need to dynamically be able to adjust the width of the colum for each datagridtextcolumn, to fit the longest entry in that column. Any ideas on how this can be accomplished?

Best Regards,
CV.
Avatar of hatem72
hatem72

Avatar of connelvalentine

ASKER

HI hatem72,

Unfortunately, i'm still using VB.NET 2002.
I understand that the datagridview control has replaced the datagrid control in later versions of VB.NET. I'm looking for a solution that will work with just a datagrid control.

OR, i'm even open to any suggestions on how it can be possible to use the datagridview control in the VB.NET version i have. If this is possible, will i have to take any precautions while distributing my application later?

Best Regards,
CV
For datagrid   net 1.1 check this solution
http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q877q

vbturbo
ASKER CERTIFIED SOLUTION
Avatar of hatem72
hatem72

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Hatem72,

This solution almost seems to be working from the dotnet247 link, but i'm having some problems understanding the code to tweak it a bit:

Dim size As SizeF
Dim sf As StringFormat = New StringFormat(StringFormat.GenericTypographic)
size = g.MeasureString(dtgr_database(i, col).ToString(), dtgr_database.Font, 500, sf)

This is just a small snippet i've taken out from the code. Could you please explain what these lines of code mean? They're pretty greek to me and the VS help wasn't too helpful.

BR,
CV.
Hi,

First thanx for [B] ... about that code snippet ... it's search result & i am working
with vb 2005 ... i am sory ... BUT good thing that snippet [almost seems to be working ] .