簡體   English   中英

如何綁定列表 <DateTime> 到GridView

[英]How to bind a List<DateTime> to a GridView

我已經閱讀了堆棧溢出問題, 如何將列表綁定到gridview?

但是我需要將List <DateTime>綁定到gridview,但主要是我還希望能夠從ItemTemplate中訪問DateTime值。 我怎么做? 當我使用DataTable時,我曾經那樣做:

<%# Eval("SDIndex") %>

如何將其直接鏈接到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()) %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM