在gridview中访问数据库时选择DateTime作为日期

时间:2014-01-15 10:09:09

标签: asp.net sql-server-2008 gridview

我有一个网格视图,我在cs页面中直接访问数据库。在数据库中我有日期格式显示为2013-07-15 00:00:00.000使用select命令,然后我有wriiten转换它以Only Date格式显示为select CONVERT(date, dojmu) as Date from Institutional_det,其输出为2013-07-15。现在我在asp.net中使用了相同的查询,但它显示为datetime但不显示为date

  if (DropDownList1.SelectedItem.Text == "Workshop")
        {
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ProjectConnectionString"].ToString());
            con.Open();
            SqlDataAdapter adapter = new SqlDataAdapter("select p.Name,m.FID as [Faculty ID],m.Type,CONVERT(date,m.Date) as Date,m.Theme,m.Duration,m.Organizer as [Role],m.UpdateDate from WorkshopSC_det m INNER JOIN Personal_det p  ON m.FID= p.FID where m.updateDate  between @Start and @End order by m.updateDate desc ", con);
            adapter.SelectCommand.Parameters.Add("@Start", SqlDbType.Date).Value = startDate;
            adapter.SelectCommand.Parameters.Add("@End", SqlDbType.Date).Value = endDate;

            DataTable dt = new DataTable();
            adapter.Fill(dt);
            con.Close();
            GridView1.DataSource = dt;

            GridView1.DataBind();}

Gridview aspx代码

 <asp:GridView ID="GridView1" runat="server" 
        RowStyle-HorizontalAlign="Center" CellPadding="4" ForeColor="#333333" 
        GridLines="None">
<AlternatingRowStyle BackColor="White" />
    <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
  <RowStyle HorizontalAlign="Center" BackColor="#FFFBD6" ForeColor="#333333"></RowStyle>
    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
    <SortedAscendingCellStyle BackColor="#FDF5AC" />
    <SortedAscendingHeaderStyle BackColor="#4D0000" />
    <SortedDescendingCellStyle BackColor="#FCF6C0" />
    <SortedDescendingHeaderStyle BackColor="#820000" />
    </asp:GridView>

任何Suggsetions都表示赞赏。

1 个答案:

答案 0 :(得分:0)

您可以将SQL select语句更新为:

select p.Name,m.FID as [Faculty ID],m.Type,convert(varchar(10), m.Date, 120) as 
Date,m.Theme,m.Duration,m.Organizer as [Role],m.UpdateDate from WorkshopSC_det m 
INNER JOIN Personal_det p  ON m.FID= p.FID where m.updateDate  between @Start 
and @End order by m.updateDate desc "