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?
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;
0
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

0

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.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

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
0
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.
0
brgdotnetcontractorAuthor Commented:
None of these resizing techniques are working for me? Is there anything else involved that I need to know about?
0
Lokesh B RDeveloperCommented:
Hi,

I think you are working on Windows forms, right?

If Windows forms the above posted code will work.
0
brgdotnetcontractorAuthor Commented:
Ok lokesh. Let me try your solution tomorrow, then I will award the points.
0
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?
0
Lokesh B RDeveloperCommented:
Hi,
Can you share the full code?
0
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.
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.