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?
LVL 2
brgdotnetcontractorAsked:
Who is Participating?

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

x
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:
Hi,

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

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.
    dataGridView.AutoResizeColumns()

Open in new window


https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.autosizecolumnsmode(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2

Setting individual column width

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

Open in new window


https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn.width(v=vs.110).aspx
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:
Hi,

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:
Hi,
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.