Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

Datagridview has a property AutoSizeColumnsMode with following options

VB
// 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.
 
Share this answer
 
Comments
NeueStudium2012 16-Dec-12 2:56am    
That works, even I expected somehow easier.
Thank you for your answer.
Jibesh 16-Dec-12 3:25am    
appreciated that solved your problem.
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"

C#
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))

}
 
Share this answer
 
v3
Comments
NeueStudium2012 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 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
StackQ 15-Dec-12 7:14am    
now check again
Shahin Khorshidnia 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
StackQ 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900