[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 561
  • 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
 
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
 
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
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:
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
 
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

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

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