-1

我在任何地方都找不到答案...我的 List<string[]> 填充如下:

...
while (myReader.Read())
{
    string[] row = new string[myInt];

    for (int i = 0; i < myInt; i++)
    {
        row[i] = myReader[i].ToString();
    }

    myList.Add(row);    
}
...

如何将此列表绑定到具有 TemplateField 列的网格视图?

4

3 回答 3

3

一种更简单的方法是创建一个匿名类并将其绑定到您的 GridView。例子:

var query = from c in row
            select new { SomeProperty = c };

GridView.DataSource=query;
GridView.DataBind();
于 2012-10-01T20:14:32.910 回答
1

您可以随时RowDataBound使用GridView

<asp:GridView ID="gridView1" runat="server" 
    OnRowDataBound="gridView1_DataBound">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="myLabel" runat="server"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

绑定数据时:

var myStrings = new List<string[]>
                      {
                          new [] { "hello", "bye"},
                          new [] { "1", "2"}
                      };

gridView1.DataSource = myStrings;
gridView1.DataBind();

RowDataBound事件:

public void gvDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType != DataControlRowType.DataRow)
    {
        return;
    }

    var item = (string[]) e.Row.DataItem;
    Label myLabel = e.Row.FindControl("myLabel") as Label;
    myLabel.Text = item[0];
}
于 2012-10-01T20:07:21.910 回答
0

像往常一样简单地使用数据绑定。要引用该列,它默认按索引进行。所以像这样:

<asp:GridView runat="server" AutoGenerateColumns="false" ID="rpt">
<Columns>
    <ItemTemplate>
        <%# Eval("Key") %>
    </ItemTemplate>
</Columns>
</asp:Repeater>

Dictionary<string, string> lst = new Dictionary<string, string>();
lst.Add("test", String.Empty);
lst.Add("test1", String.Empty);
this.rpt.DataSource = lst;
this.rpt.DataBind();
于 2012-10-01T19:59:20.693 回答