Resie the width of columns in a datagridview

Hi,

I have a datagridview with many columns and rows of data...

I just want each column width to be resized to the width of the longest value within that column (including headers) ...

How do I do this...?

Thanks...
LVL 1
ISCAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
ISCConnect With a Mentor Author Commented:
Hi,

This works for me..

Me.dgv_stif_assets.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
Try this:
            Me.DataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.ColumnHeader)
            ' or
            Me.DataGridView1.AutoResizeColumnHeadersHeight()

Open in new window

0
 
ISCAuthor Commented:
Hi,

I tired this and the column width still does not adjust to the longest text in the column...

Thanks  
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
srikanthreddyn143Commented:
You can use a logic in this way

1. Get all columns width into an arraylist

2. Sort the arraylist

3. get the maximum value

4. Assign to all columns

Mean while i will write the code
0
 
srikanthreddyn143Commented:
This is just an idea. I didnt check it. it maight have compiler errors.

Dim alWidth As New Arraylist

For Each oCol As GridviewColumn In GrdView.Columns

alwidth.Add(oCol.Width)
Next
alwidth.Sort()
Dim iMaxWidth As Integer = alwidth.item(alwidth.count - 1)

For Each oCol As GridviewColumn In GrdView.Columns

oCol.Width = iMaxWidth
Next
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
I tired this and the column width still does not adjust to the longest text in the column...
Is it solved already, but where did you tried my examples ? Did you tried after fill the grid ?
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
My solution also works
Me.DataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.ColumnHeader)
You just need to change to what you want. I have used ColumnHeader but you could change to AllCells
Thats a little unfair but it's your decision.
 
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.