Click here to Skip to main content
11,718,016 members (84,458 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hello everybody,
I am new in programming after a long period of 10 years without writing a line and I am learning c# by myself.

I am dealing with Forms and Columns and I can not get the way of setting a fixed width for all the columns in a Grid View.

Can anyone help me?

Thank you in advance.
David
Posted 15-Dec-12 0:51am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Datagridview has a property AutoSizeColumnsMode with following options

// Summary:
//     Defines values for specifying how the widths of columns are adjusted.
public enum DataGridViewAutoSizeColumnsMode
{
   //Summary:The column widths do not automatically adjust.
   None = 1,
   //
   //Summary:The column widths adjust to fit the contents of the column header cells.
   ColumnHeader = 2,
   // Summary:
   //The column widths adjust to fit the contents of all cells in the columns,excluding header cells.
   AllCellsExceptHeader = 4,
   //
   // Summary:
   //The column widths adjust to fit the contents of all cells in the columns,including header cells.
   AllCells = 6,
   //
   //Summary:The column widths adjust to fit the contents of all cells in the columns
   //that are in rows currently displayed onscreen, excluding header cells.
   DisplayedCellsExceptHeader = 8,
   //
   // Summary:
   //The column widths adjust to fit the contents of all cells in the columns
   //that are in rows currently displayed onscreen, including header cells.
   DisplayedCells = 10,
   //
   // Summary:
   //The column widths adjust so that the widths of all columns exactly fill the
   //display area of the control, requiring horizontal scrolling only to keep
   //column widths above the System.Windows.Forms.DataGridViewColumn.MinimumWidth
   //property values. Relative column widths are determined by the relative System.Windows.Forms.DataGridViewColumn.FillWeight
   //property values.
   Fill = 16,
  }

set the property :AutoSizeColumnsMode=ColumnHeader it will auto resize your column based on the Column Header string width.
  Permalink  
Comments
NeueStudium2012 at 16-Dec-12 2:56am
   
That works, even I expected somehow easier.
Thank you for your answer.
jibesh at 16-Dec-12 3:25am
   
appreciated that solved your problem.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

GridView1.Columns[i].ItemStyle.Width = colWidth;

2nd Solution:::::::
step1:-Create a function,given below

public DataGridViewTextBoxColumn GetDataGridViewColumn(ref DataGridView DGV, string colName, string colHeader, int colWidth, bool colVisibility = true, string DataPropertyName = "")
{
	DataGridViewTextBoxColumn DGVTbCol = new DataGridViewTextBoxColumn();
	DGVTbCol.Name = colName;
	if (string.IsNullOrEmpty(DataPropertyName)) {
		DGVTbCol.DataPropertyName = colName;
	} else {
		DGVTbCol.DataPropertyName = DataPropertyName;
	}
 
	DGVTbCol.HeaderText = colHeader;
	DGVTbCol.DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft;
	DGVTbCol.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
	DGVTbCol.Width = colWidth;
	DGVTbCol.Visible = colVisibility;
	if (colVisibility) {
		DGV.Tag += colWidth;
	}
	return DGVTbCol;
}


2nd Step:-
String C_Id = "1stColumnName"
String C_Id2="2ndColumnName"
private void SetColumns(ref DataGridView DGV)
{
    int AlignLeft = 0;
    int AlignRight = 1;
    int AlignCenter = 2;
    int ControlStart = 0;
    int CtrlTop = 0;
    int ColWidth = 0;
 
    DGV.AutoGenerateColumns = false;
    DGV.MultiSelect = true;
    DGV.ReadOnly = false;
    DGV.Tag = 0;
    DGV.Columns.Clear();
ColWidth=70; //Specify width as u want
  DGV.Columns.Add(GetDataGridViewColumn(DGV, C_Id, "ColumnName", ColWidth, False))
DGV.Columns.Add(GetDataGridViewColumn(DGV, C_Id2, "2ndColumnName",ColWidth, True))
 
}
  Permalink  
v3
Comments
NeueStudium2012 at 15-Dec-12 7:12am
   
Thank you, but does it is not what I meant. Loops or individual assignment are not the solution which I look for.
I think there must be a way to define the column width for all the grid, as default or something likely, but I am not able to find it.
Nelek at 15-Dec-12 8:19am
   
Poster updated the solution but didn't reply your comment (just added a new one, so no notification for you). Have a look to the new code
01010RAJ at 15-Dec-12 7:14am
   
now check again
Shahin Khorshidnia at 15-Dec-12 8:52am
   
Hi, I didn't vote but please pay attention to your notation. May I suggest you this: C# Coding Standards
01010RAJ at 15-Dec-12 11:17am
   
actually i create it on vb.net,and for u,i convert them to vb online.So just follow logic.Thnx to suggest me...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 215
1 F-ES Sitecore 210
2 Maciej Los 140
3 Jochen Arndt 120
4 Andy Lanng 115
0 Sergey Alexandrovich Kryukov 1,649
1 OriginalGriff 535
2 Maciej Los 529
3 Richard MacCutchan 440
4 CHill60 415


Advertise | Privacy | Mobile
Web02 | 2.8.150901.1 | Last Updated 15 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100