我无法在任何地方找到答案...我有一个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列将此列表绑定到gridview?
答案 0 :(得分:3)
更简单的方法是创建一个匿名类并将其绑定到GridView。例如:
var query = from c in row
select new { SomeProperty = c };
GridView.DataSource=query;
GridView.DataBind();
答案 1 :(得分: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];
}
答案 2 :(得分: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();