How to size GridView columns

What is the best way to size GridView columns? My GridViews will only have two or three columns at most. So how do I size them?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Naitik GamitSoftware DeveloperCommented:
You Can set GridView columns Size As:

DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;

Or Set Programatically:

DataGridViewColumn column = dataGridView.Columns[0];
column.Width = 100;
Lokesh B RDeveloperCommented:

1. Static setting

In ASPX you can set the width.

<asp:BoundField DataField="Description" HeaderText="Item Description" ItemStyle-Width="300px" />

Open in new window

2. Dynamic Setting

Add the RowDataBound event on your GridView.

Protected Sub GrdView1_RowDataBound(sender As Object, e As GridViewRowEventArgs)
     If e.Row.RowType = DataControlRowType.DataRow Then
          grdListings.Columns(0).ItemStyle.Width = 150
          grdListings.Columns(1).ItemStyle.Width = 250
          grdListings.Columns(2).ItemStyle.Width = 350
      End If
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
brgdotnetcontractorAuthor Commented:
Hi Lokesh. I am developing for Winform applications, not the web. I will check out your code anyway.
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Lokesh B RDeveloperCommented:

In windows forms as below.

 ' Bind the DataGridView data from the database.
    dataGridView.DataSource = masterBindingSource
    ' Resize the DataGridView columns to fit the newly loaded data.

Open in new window

Setting individual column width

dataGridView.Columns(0).Width = 150
dataGridView.Columns(1).Width = 250
dataGridView.Columns(2).Width = 350

Open in new window
brgdotnetcontractorAuthor Commented:
Thank you. I will award the points tomorrow, after I test it out. The first couple of solutions did now work for me, and I am trying to figure out why.
brgdotnetcontractorAuthor Commented:
None of these resizing techniques are working for me? Is there anything else involved that I need to know about?
Lokesh B RDeveloperCommented:

I think you are working on Windows forms, right?

If Windows forms the above posted code will work.
brgdotnetcontractorAuthor Commented:
Ok lokesh. Let me try your solution tomorrow, then I will award the points.
brgdotnetcontractorAuthor Commented:
Lokesh it is not working for me. I am still not able to resize the controls. Does this have to be done in the main method of the form?
Lokesh B RDeveloperCommented:
Can you share the full code?
brgdotnetcontractorAuthor Commented:
No I can't. But I will just award the points and try and figure it out some more on my own. If I make more progress and have a better question, I will post it.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.