Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 558
  • Last Modified:

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...
0
ISC
Asked:
ISC
  • 3
  • 2
  • 2
1 Solution
 
jpaulinoCommented:
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
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
ISCAuthor Commented:
Hi,

This works for me..

Me.dgv_stif_assets.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
0
 
jpaulinoCommented:
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
 
jpaulinoCommented:
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

Featured Post

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.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now