Solved

Resie the width of columns in a datagridview

Posted on 2009-05-15
7
549 Views
Last Modified: 2012-05-07
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
Comment
Question by:ISC
  • 3
  • 2
  • 2
7 Comments
 
LVL 48

Expert Comment

by:jpaulino
Comment Utility
Try this:
            Me.DataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.ColumnHeader)

            ' or

            Me.DataGridView1.AutoResizeColumnHeadersHeight()

Open in new window

0
 
LVL 1

Author Comment

by:ISC
Comment Utility
Hi,

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

Thanks  
0
 
LVL 11

Expert Comment

by:srikanthreddyn143
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 11

Expert Comment

by:srikanthreddyn143
Comment Utility
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
 
LVL 1

Accepted Solution

by:
ISC earned 0 total points
Comment Utility
Hi,

This works for me..

Me.dgv_stif_assets.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
0
 
LVL 48

Expert Comment

by:jpaulino
Comment Utility
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
 
LVL 48

Expert Comment

by:jpaulino
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now