Assume you got a gridview, you extract the data from database. when the data[datetime] is min value, then the display "-". Blow is my test code to display "-", when the datetime is “1/1/2001 12:00:00 AM”. Hope it helps.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
namespace WebDev
{
public partial class WebForm18 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string connString = ConfigurationManager.ConnectionStrings["Test_dbConnectionString"].ToString();
SqlConnection sqlcon = new SqlConnection(connectionString: connString);
sqlcon.Open();
SqlCommand command = new SqlCommand();
command.CommandText = "SELECT * FROM Table_6 ";//two columns[datetime,note]
command.Connection = sqlcon;
SqlDataReader reader = command.ExecuteReader();
GridView1.DataSource = reader;
GridView1.DataBind();
}
protected void GridView1_DataBound(object sender, EventArgs e)
{
//Response.Write(GridView1.Rows.Count);
//Response.Write(GridView1.Columns.Count);
//Response.Write(DateTime.MinValue); //DateTime.MinValue=1/1/0001 12:00:00 AM
/* gridview colummn 1 is datatime you got from database */
for (int i = 0; i < GridView1.Rows.Count;i++ )
{
if (GridView1.Rows[i].Cells[0].Text == "1/1/2001 12:00:00 AM")// DateTime.MinValue
{
GridView1.Rows[i].Cells[0].Text = "-";//display "-", if datetime is min value
}
}
}
}
}
Marked as answer by Ming Xu - MSFT on Apr 30, 2012 02:35 PM
ramll
Participant
1126 Points
1299 Posts
Display Hyphen, if DateTime is min
Apr 19, 2012 02:17 PM|LINK
Hello,
I want to display "-", if datetime is min value
Here is my code. How can I display Hyphen in Date Grid Column.
If obj.ExpirationDate = DateTime.MinValue Then obj.ExpirationDate = "Display Hyphen Here" End Iframll
Participant
1126 Points
1299 Posts
Re: Display Hyphen, if DateTime is min
Apr 19, 2012 05:13 PM|LINK
Anyone ?
Mark-yu
Participant
888 Points
127 Posts
Re: Display Hyphen, if DateTime is min
Apr 23, 2012 06:53 AM|LINK
Hi,
I don't understand you very clear, could you post an display example here ? here is some about datatime format, refer link:http://www.csharp-examples.net/string-format-datetime/ hope it helps.
richkyrs
Member
436 Points
500 Posts
Re: Display Hyphen, if DateTime is min
Apr 23, 2012 07:15 AM|LINK
lblMessage.Text. = "welcome" + tbName.Text + "!<br>it" + "is" + System.DataTime.Now.ToString();
and for source code on behind design you would d this for username plus date and time
<form runat="server">
Please enter a user name:<p>
<asp:TextBox ID="tbName" runat="server" />
<asp:Button ID="btSubmit" text="Submit" OnClick="submit" runat="server" /><p>
<asp:Label ID="lblMessage" runat="server" />
</form>
</body></html>
richkyrs
Member
436 Points
500 Posts
Re: Display Hyphen, if DateTime is min
Apr 23, 2012 09:57 AM|LINK
oops slight error this should be fine
<div>lblMessage.Text = "welcome" + tbName.Text + "!<br>it" + "is" + System.DataTime.Now.ToString();
and for source code on behind design you would d this for username plus date and time
<form runat="server">
Please enter a user name:<p>
<asp:TextBox ID="tbName" runat="server" />
<asp:Button ID="btSubmit" text="Submit" OnClick="submit" runat="server" /><p>
<asp:Label ID="lblMessage" runat="server" />
</form>
</body></html>
</div>richkyrs
Member
436 Points
500 Posts
Re: Display Hyphen, if DateTime is min
Apr 23, 2012 09:58 AM|LINK
dont add div tags dont no how they ended up there
Mark-yu
Participant
888 Points
127 Posts
Re: Display Hyphen, if DateTime is min
Apr 27, 2012 03:01 AM|LINK
Hi ramll,
Assume you got a gridview, you extract the data from database. when the data[datetime] is min value, then the display "-". Blow is my test code to display "-", when the datetime is “1/1/2001 12:00:00 AM”. Hope it helps.
.aspx file code:-
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm18.aspx.cs" Inherits="WebDev.WebForm18" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ondatabound="GridView1_DataBound"> <Columns> <asp:BoundField HeaderText="DateTime" SortExpression="DateTime" DataField="DateTime"/> <asp:BoundField HeaderText="Note" SortExpression ="Note" DataField="Note" /> </Columns> </asp:GridView> </div> </form> </body> </html>.cs file code:-
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Configuration; using System.Data.SqlClient; namespace WebDev { public partial class WebForm18 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string connString = ConfigurationManager.ConnectionStrings["Test_dbConnectionString"].ToString(); SqlConnection sqlcon = new SqlConnection(connectionString: connString); sqlcon.Open(); SqlCommand command = new SqlCommand(); command.CommandText = "SELECT * FROM Table_6 ";//two columns[datetime,note] command.Connection = sqlcon; SqlDataReader reader = command.ExecuteReader(); GridView1.DataSource = reader; GridView1.DataBind(); } protected void GridView1_DataBound(object sender, EventArgs e) { //Response.Write(GridView1.Rows.Count); //Response.Write(GridView1.Columns.Count); //Response.Write(DateTime.MinValue); //DateTime.MinValue=1/1/0001 12:00:00 AM /* gridview colummn 1 is datatime you got from database */ for (int i = 0; i < GridView1.Rows.Count;i++ ) { if (GridView1.Rows[i].Cells[0].Text == "1/1/2001 12:00:00 AM")// DateTime.MinValue { GridView1.Rows[i].Cells[0].Text = "-";//display "-", if datetime is min value } } } } }