如何绑定List< DateTime>到GridView [英] How to bind a List<DateTime> to a GridView

查看:67
本文介绍了如何绑定List< DateTime>到GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了堆栈溢出问题 如何绑定到gridview的列表? .

I already read Stack Overflow question How to bind a List to a gridview?.

但是我需要将List< DateTime>绑定到gridview,但主要是我还希望能够从ItemTemplate中访问DateTime值.我怎么做?当我使用DataTable时,我曾经那样做:

But I need to bind a List<DateTime> to a gridview, but MAINLY I also want be able to access the DateTime value from within a ItemTemplate. How do I do that? When I was working with a DataTable, I used to do it like that:

<%# Eval("SDIndex") %>

如何将其直接链接到List< DateTime>?

How would I do it linking it directly to the List<DateTime>?

推荐答案

您可以尝试以下操作:

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Linq" %>
<script type="text/C#" runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        var dates = Enumerable.Range(1, 20).Select(x => new DateTime(2011, 4, x)).ToList();
        grd.DataSource = dates;
        grd.DataBind();
    }
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="Form1" runat="server">
    <asp:GridView ID="grd" runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <%# ((DateTime)GetDataItem()).ToString("dd/MM/yyyy") %>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    </form>
</body>
</html>

这也应该起作用:

<%# string.Format("{0:dd/MM/yyyy}", GetDataItem()) %>

这篇关于如何绑定List&lt; DateTime&gt;到GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆