Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Resie the width of columns in a datagridview

Posted on 2009-05-15
7
552 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
ID: 24394664
Try this:
            Me.DataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.ColumnHeader)
            ' or
            Me.DataGridView1.AutoResizeColumnHeadersHeight()

Open in new window

0
 
LVL 1

Author Comment

by:ISC
ID: 24395557
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
ID: 24396169
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 11

Expert Comment

by:srikanthreddyn143
ID: 24396261
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
ID: 24396791
Hi,

This works for me..

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

Expert Comment

by:jpaulino
ID: 24404174
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
ID: 24450850
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

808 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