0

我有一个带有 LinqtoSQL 数据源的 GridView。我在代码中绑定它。这是代码

 DataClassesDataContext dContext = new DataClassesDataContext();
            var userId = (Guid)(Membership.GetUser(Membership.GetUser().UserName, false).ProviderUserKey);
            var Query = from d in dContext.table1
                              where d.UserId == userId
                              select new { d.date, d.weight, d.height, d.BMI };


            gv.DataSource = Query;
            gv.DataBind();

d.date 是 DateTime 列我喜欢只显示日期如何格式化它?谢谢

4

3 回答 3

1

aspx 页面中的代码

<asp:GridView ID="gridView1" runat="server" AutoGenerateColumns="false">
                <Columns>
                    <asp:BoundField DataField="Login_USER" HeaderText="Login User" />
                    <asp:BoundField DataField="Login_DATE" HeaderText="Login Date"
                        DataFormatString="{0:MM/dd/yyyy}" />
                </Columns>
            </asp:GridView>

=======================================

代码

Entities db = new Entities();

var users = from user in db.Logged_user select new {user.Login_USER, user.Login_DATE };

if(users != null)
{
     gridView1.DataSource = branches.ToList();
     gridView1.DataBind();
}
于 2015-03-01T11:55:57.420 回答
0

将 DateTime 转换为仅显示日期的方法之一就是这个

DateTime today=DateTime.Now;
string todayString=today.ToString();
string dateOnly=todayString.Split(' ')[0];
于 2012-07-21T23:07:54.923 回答
0
var Query = from d in dContext.table1 
            where d.UserId == userId 
            select new { date = String.Format("{0:MM/dd/yyyy}", d.date), d.weight, d.height, d.BMI };
于 2012-08-08T12:14:04.167 回答