DateFormat problem
-
Hello, I am using Sql server, I want to display a date on a datagrid with the following details: _ Only the date and not the time _ The format of the date should be dd/mm/dddd I am getting this column from the database and would like to display on a datagrid.... can someone help with the sql statement that will help me in doing this conversion? Thanks alot, Commickey
-
Hello, I am using Sql server, I want to display a date on a datagrid with the following details: _ Only the date and not the time _ The format of the date should be dd/mm/dddd I am getting this column from the database and would like to display on a datagrid.... can someone help with the sql statement that will help me in doing this conversion? Thanks alot, Commickey
//Check the third field , this is how u need to write 103 represents some code given by .net string query = "select build_desc,baseline,folder,convert(varchar(10),build_date,103),comment,developed_by,convert(varchar(10),modified_on,103),approval_flag from build_master order by build_date"; Hope this helps Jiny -- modified at 5:59 Thursday 29th June, 2006
-
Hello, I am using Sql server, I want to display a date on a datagrid with the following details: _ Only the date and not the time _ The format of the date should be dd/mm/dddd I am getting this column from the database and would like to display on a datagrid.... can someone help with the sql statement that will help me in doing this conversion? Thanks alot, Commickey
Hi, Use string.Format("{o:dd/MM/yyyy}",yourdate); check whether mm or MM Thanks, Sushant Duggal. -- modified at 5:49 Thursday 29th June, 2006
-
Hello, I am using Sql server, I want to display a date on a datagrid with the following details: _ Only the date and not the time _ The format of the date should be dd/mm/dddd I am getting this column from the database and would like to display on a datagrid.... can someone help with the sql statement that will help me in doing this conversion? Thanks alot, Commickey
I don't know how you can do date conversion in SQL, but you can use the following code to do this in page code:
// Language = C# // dgrd = DataGrid // change cell index to cell index for your datagrid foreach (DataGridItem item in dgrd.Items) { DateTime date = DateTime.Parse(item.Cells\[1\].Text); item.Cells\[1\].Text = date.ToShortDateString(); }
This sould work, but if result is not in dd/mm/yyyy format then use the following:
// Language = C# // dgrd = DataGrid // change cell index to cell index for your datagrid foreach (DataGridItem item in dgrd.Items) { DateTime date = DateTime.Parse(item.Cells\[1\].Text); string text = date.Day + "/" + date.Month + "/" + date.Year; item.Cells\[1\].Text = text; }
Just make sure data type for the column in database is datetime. If it is not, use a try...catch loop when parsing string to prevent error. Regards, lordfkiller
-
//Check the third field , this is how u need to write 103 represents some code given by .net string query = "select build_desc,baseline,folder,convert(varchar(10),build_date,103),comment,developed_by,convert(varchar(10),modified_on,103),approval_flag from build_master order by build_date"; Hope this helps Jiny -- modified at 5:59 Thursday 29th June, 2006
-
I don't know how you can do date conversion in SQL, but you can use the following code to do this in page code:
// Language = C# // dgrd = DataGrid // change cell index to cell index for your datagrid foreach (DataGridItem item in dgrd.Items) { DateTime date = DateTime.Parse(item.Cells\[1\].Text); item.Cells\[1\].Text = date.ToShortDateString(); }
This sould work, but if result is not in dd/mm/yyyy format then use the following:
// Language = C# // dgrd = DataGrid // change cell index to cell index for your datagrid foreach (DataGridItem item in dgrd.Items) { DateTime date = DateTime.Parse(item.Cells\[1\].Text); string text = date.Day + "/" + date.Month + "/" + date.Year; item.Cells\[1\].Text = text; }
Just make sure data type for the column in database is datetime. If it is not, use a try...catch loop when parsing string to prevent error. Regards, lordfkiller
-
This is what I usually do: The code should be executed after DataGrid is data bound(DataGrid.DataBind() is called). So that it can edit DataGrid items. In post backs, you don't need to data bind DataGrid, because it is already data bound (in load), but you may need to run converting code in post backs, too. To do so, data bind DataGrid in page_load (use if(!IsPostBack)). Place code for modifying DataGrid in a separate method and call it in page_load (outside if(!IsPostBack)). If you are using ASP.NET 2, note that when you do data binding using a DataSource, you still need to call DataGrid.DataBind() in page_load. Because this method is called after page_load in page life cycle and you need to work on DataGrid in page_load, you must call it explicitly. But using ItemDataBound, it is much better and easier:
protected void dgrdWebSites\_ItemDataBound(object s, DataGridItemEventArgs e) { DateTime date = DateTime.Parse(e.Item.Cells\[1\].Text); string text = date.Day + "/" + date.Month + "/" + date.Year; e.Item.Cells\[1\].Text = text; }
Nothing more is needed! Let me know if you need more explanation. Best regards, lordfkiller