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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to display date/time value in a datagrid 3

Status
Not open for further replies.

vatawna

Programmer
Feb 24, 2004
67
US
I have a datagrid whose datasource is set equal to a dataset. One of the columns in this dataset is createDate which is of type smalldatetime on SQL server. When data is displayed in the datagrid, the createDate values are displayed as dates only, but no time is shown. I tested the output of createDate by outputing its values to a label control to see what is sent back to the dataset from the database. Surely the label shows date and time.

Does anyone know what the problem is?

Thanks.
 
Question :
I have a datagrid whose datasource is set equal to a dataset. One of the columns in this dataset is createDate which is of type smalldatetime on SQL server. When data is displayed in the datagrid, the createDate values are displayed as dates only, but no time is shown. I tested the output of createDate by outputing its values to a label control to see what is sent back to the dataset from the database. Surely the label shows date and time.

Does anyone know what the problem is?

Vatawana, I am not sure if you still need an answer, but I successfully show the datetime in my datagrid by doing the following :

In the private void InitClass() in your code behind, make sure when you add the column to the datagrid it is using
System.DateTime as the type :

this.columncreate_date = new DataColumn("create_date", typeof(System.DateTime), null, System.Data.MappingType.Element);
this.Columns.Add(this.columncreate_date);

This worked for me.

 
Code:
private void _FormatGridStyle2(ref System.Windows.Forms.DataGrid dg, DataTable dtMapping)
{
	dg.TableStyles.Clear();
	DataGridTableStyle gs = new DataGridTableStyle();
	gs.MappingName = dtMapping.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 dtMapping.Columns)
	{
		DataGridTextBoxColumn vColumnStyle= new DataGridTextBoxColumn();
		vColumnStyle.HeaderText = vColumn.ColumnName;
		vColumnStyle.MappingName = vColumn.ColumnName ;
		vColumnStyle.Alignment =  System.Windows.Forms.HorizontalAlignment.Center ; 
		vColumnStyle.Width = 60;
		gs.GridColumnStyles.Add(vColumnStyle);
	}
	dg.TableStyles.Add(gs);  
				
}
Here is an example.
The _tSumary table has a column "creation" which is a DateTime type in the database.
Use a DataGridTableStyle to format the grid as an example is in the above function and you will see the DateTime columns are displayed with date & time.
What you have to do is to write a function like _FormatGridStyle2() and call it before yoy call SetDataBinding() on the grid.
Code:
DataGrid dataGrid1 = new DataGrid();
DataTable dt = new DataTable();

sConntring = "SELECT * from _tSummary";
SqlConnection sConn		= new SqlConnection(sConnString);
SqlCommand WorkingCommand 	= new SqlCommand();
SqlDataAdapter WorkingDataAdapter= new SqlDataAdapter();
sConn.Open();
WorkingCommand.Connection 	= sConn;
WorkingCommand.CommandText      = sqlStatement;
WorkingDataAdapter.SelectCommand = WorkingCommand;
WorkingDataAdapter.FillSchema (dt,SchemaType.Source);
WorkingDataAdapter.Fill(dt);

_FormatGridStyle2(ref dataGrid1,dt);
dataGrid1.SetDataBinding(dt, "Summary");
dataGrid1.CaptionText = dt.Rows.Count + " Record(s) - " ;
dataGrid1.GridLineStyle = DataGridLineStyle.Solid;
dataGrid1.Show();
-obislavu-

 
This is something the I have encountered also.
Well I am not sure , but if you increase the width of the
column enugh to occupy the required datetime the (date + time) both gets displayed on the grid.

Hope this helps

Regards
Nikhil
 
No, you should use DataGridTableStyle as I explained in my previous post.
-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top