如何将列表绑定到gridview?

时间:2013-03-06 22:01:16

标签: c# json gridview

我正在尝试使用JSON文档,将其放入列表中,并将其绑定到gridview。我尝试了几种不同的方法,但没有任何作用。这是我的代码,问题在最后4行。

   protected void Page_Load(object sender, EventArgs e)
    {


        var from = origin.Text;
        var to = destination.Text;
        var requesturl = @"http://maps.googleapis.com/maps/api/directions/json?origin =" + from + "&alternatives=false&units=imperial&destination=" + to + "&sensor=false;";
        string content = file_get_contents(requesturl); 


        JavaScriptSerializer jss = new JavaScriptSerializer();
        var d = jss.Deserialize<dynamic>(content);
        List<string> sl = d;

        GridView1.DataSource = content;
        GridView1.DataBind();



    }

1 个答案:

答案 0 :(得分:0)

以下是列出这些指示的两种方法:

1 - 使用GridView:

<asp:GridView ID="grdExample" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField HeaderText="Directions">
            <ItemTemplate>
                <%# Container.DataItem %>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

绑定GridView:

List<string> strings = new List<string>() { "Ha!", "Hey!", "Yo!" };

grdExample.DataSource = strings;
grdExample.DataBind();

2 - 使用转发器(更简单,更灵活,需要您自己的HTML):

<asp:Repeater ID="rptExample" runat="server" >
    <ItemTemplate>
        <%# Container.DataItem %>
    </ItemTemplate>
</asp:Repeater>

绑定它:

List<string> strings = new List<string>() { "Ha!", "Hey!", "Yo!" };

rptExample.DataSource = strings;
rptExample.DataBind();