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 control

Status
Not open for further replies.

Katy44

Technical User
Dec 12, 2003
723
GB
Hi,

I hope I'm asking this in the right place but I am new to .NET and a bit out of my depth!
I have a datagrid whose datasource is a dataset, created from database tables. The element name is simply the field headings from my tables and that's fine. However, when the datagrid is displayed, I want the 'headings' to be different - I want to be able to use spaces and format them nicely etc. How do I do this?

Thank you for any help
 
DataGridTextBoxColumn vColumnStyle= new DataGridTextBoxColumn();
or
DataGridBoolColumn vColumnStyle= new DataGridBoolColumn();
and then for ecah column set the Header text, width and other properties:
vColumnStyle.HeaderText = "Blablabla";

Let the following case:

dtMapping a table which mapp the columns to be diplayed in a grid:
==========================================
fn ln bdate
First Name Last Name Birth Date
==========================================

dtBound - a DataTable which is used to bind the datagrid. This table has the following columns: "fn","ln", "bdate".
The DataGrid will display : "First Name" , "Last name " and " Birth Date" read from the dtMapping which is also loaded from a source table.

Code:
private void _FormatGridStyle2(ref System.Windows.Forms.DataGrid dg, DataTable dtBound , DataTable dtMapping)
{
	dg.TableStyles.Clear();
	DataGridTableStyle gs = new DataGridTableStyle();
	gs.MappingName = dtBound.TableName;
	gs.AlternatingBackColor=System.Drawing.Color.Bisque;
	gs.AlternatingBackColor=System.Drawing.Color.LightYellow;
	gs.BackColor = System.Drawing.Color.White;
	gs.GridLineStyle=System.Windows.Forms.DataGridLineStyle.Solid;
	foreach(DataColumn vColumn in dtBound.Columns)
	{
		DataGridTextBoxColumn vColumnStyle= new DataGridTextBoxColumn();
		vColumnStyle.HeaderText = dtMapping.Rows[0][vColumn.ColumnName].ToString();
		vColumnStyle.MappingName = vColumn.ColumnName ;
		vColumnStyle.Alignment =  System.Windows.Forms.HorizontalAlignment.Center ; 
		vColumnStyle.Width = 60;
		gs.GridColumnStyles.Add(vColumnStyle);
	}
	
	dg.TableStyles.Add(gs);  
				
}
-obislavu-
 
Thank you.
I have started using a ListView in place of my datagrid, but I may have to give up on that and try datagrids again, so I'll let you know
 
You can also do it in a simpler way, if you're use C# in a Microsoft Enviroment, Simply do the following....
Define in the resource area of the DataGrid a new styles in the TableStyle Set. In tha Dialog Box that appears you put in the MappimgName field the alias of your DataTable.....
In the GridcolumnStyle set define the columns you want in the table Remembering to put in the mapping name the field name of your table and in the Header text the text you want. and thats all..
This procedure Creates the code above that obislavu wrote, but it's more simple to do...
 
Hi I am new to VB.Net and i am having exactly this problem.
I have tried to populate a form with a datagrig Oledbdataconnection ,oledbdataadaptor and a dataset in code and i have tred to do it the visual way.
setting the datastyles etc

Neither of these work!
I get the grid to fill with my table set up from an sql statement but the columns auto format still?

I assume i am doing something stupid.
Can anyone help me out please

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top