Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGridView - how to set column width?

I have a WinForms application with DataGridView control. My control has five columns (say "Name", "Address", "Phone" etc)

I am not happy with default column width. I want to have more control over column appearance. What I want is to be able to do one of the following:

  • Set width of each column in percent
  • Set width of each column in pixels
  • Use some other best-practive method (make width to fit text etc)

Please suggest - which property to use and how.

like image 245
Captain Comic Avatar asked Jan 28 '10 11:01

Captain Comic


People also ask

How to set column width in DataGridView?

To adjust column widths programmatically, use the AutoResizeColumn or AutoResizeColumns methods or set the column Width property. For more information about content-based automatic sizing, see Sizing Options in the Windows Forms DataGridView Control.

How set DataGridView size in C#?

To create fill-mode columns for values of varying size and importance. Set the DataGridView. AutoSizeColumnsMode property to Fill to set the sizing mode for all columns that do not override this value. Set the FillWeight properties of the columns to values that are proportional to their average content widths.

How set DataGridView size in VB net?

Set the property AutoSizeColumnsMode to be Fill . That'll ensure your columns stretch to 100% within your DataGridView. The columns will stretch/shrink when you resize your grid (if your grid is anchored to your form). Show activity on this post.


2 Answers

You can use the DataGridViewColumn.Width property to do it:

DataGridViewColumn column = dataGridView.Columns[0]; column.Width = 60; 

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn.width.aspx

like image 200
Bhaskar Avatar answered Sep 28 '22 03:09

Bhaskar


The following also can be tried:

DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells 

or use the other setting options in the DataGridViewAutoSizeColumnsMode Enum

like image 32
Mark Avatar answered Sep 28 '22 02:09

Mark