Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

datagrid column width 1

Status
Not open for further replies.

lfassi

Programmer
Mar 27, 2003
7
US
I need to autosize the column.width of my datagrid, for each column.
I work with windows form and c#.
Thanks
 
The code below assume the grid is already populated with data.
The DataGrid object has DataTableStyles collection.
Iterate through myGrid.TableStyles[0].GridColumnStyles
Code:
foreach(DataGridColumnStyle vColumnStyle in myGrid.TableStyles[0].GridColumnStyles )
{
if (vColumnStyle.HeaderText.ToLower()=="mycolumn")
	{
		
		//vColumnStyle.HeaderText = "";
		//vColumnStyle.MappingName = "";
		//vColumnStyle.Alignment =  System.Windows.Forms.HorizontalAlignment.Center ; 
		vColumnStyle.Width = 60;
		
	}
}
If you want to format the grid before it is populated then you have to create a DataTableStyle:
-Iterate through the DataTable.Columns
-for each DataColumn build a DataGridColumnStyle object (colud be DataGridBoolColumn or DataGridTextColumn or custom)
-set propoerties such : name, allignment, width
- add it to the DataTableStyle object
-add the DataTableStyle to myGrid.TableStyles collection.
obislavu
 
ok, but I don’t put a number in with property. I need to take the length of the Data Column.
For example if my Data Column takes this values on each row:
"1"
"222"
"3333333"
"4"
I need that de property with is the max length.
I hope you understand me.
Please, sorry for my little English.
Thanks.
 
I understood you.
If the DataColumn is a type System.String then you can use the size of the corresponding field from the datasource table.
If the DataColumn is a System.DateTime type then you should decide the format to display and so the size.
If the DataColumn is a System.Decimal type then you should also decide the length.
Also, a DataColumn could be a CheckBox and you see below the code how to set the HeaderText and the width to format the grid.
For all columns of System.String type you can retrieve the size of the corresponding field by calling
the FillSchema() method on the DataAdapter object:
Code:
DataTable dt = new DataTable();
workingDataAdapter.FillSchema(dt, SchemaType.Source);
workingDataAdapter.Fill(dt);
dataGrid1.TableStyles.Clear(); 
DataGridTableStyle vStyle = new DataGridTableStyle();
foreach(DataColumn vColumn in dt.Columns)
{
	
	if (vColumn.DataType.ToString().ToLower()=="system.string")
	{
		DataGridTextColumn vColumnStyle= new DataGridTextColumn();
		vColumnStyle.AllowNull = false;
		vColumnStyle.HeaderText = vColumn.ColumnName;
		vColumnStyle.MappingName = vColumn.ColumnName ;
		//vColumnStyle.Alignment =  System.Windows.Forms.HorizontalAlignment.Center ; 
		vColumnStyle.Width = vColumn.MaxLength;
		vStyle.GridColumnStyles.Add(vColumnStyle);
	}
	if (vColumn.DataType.ToString().ToLower() == "system.datetime")
	{
	  //..
	  
	}
	if (vColumn.DataType.ToString().ToLower() == "system.decimal")
	{
	  //..
		  
	}
	if (vColumn.DataType.ToString().ToLower() == "system.boolean") // checkbox
	{
		DataGridBoolColumn vColumnStyle= new DataGridBoolColumn();
		vColumnStyle.Alignment =  System.Windows.Forms.HorizontalAlignment.Center ; 
		vColumnStyle.Width = 10;
		vStyle.GridColumnStyles.Add(vColumnStyle);
		
	}
}
dataGrid1.TableStyles.Add(vStyle); 
dataGrid1.SetDataBinding(dt);
obislavu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top