我想从 DataTable 绑定网格视图
var dt = new DataTable();
dt.Columns.Add("nrtest");
dt.Columns.Add("asd");
dt.Columns.Add("dsa");
dt.Columns.Add("qwe");
dt.Columns.Add("ewq");
dt.Rows.Add("test1","","","","");
dt.Rows.Add("test2","","","","");
dt.Rows.Add("test3","","","","");
mygrid.DataSource = dt;
mygrid.DataBind();
我的网格中的第一列包含标签
我想将数据表中的第一列添加到gridview的第一列
我正在尝试这样:
foreach(GridViewRow gdRow in mygrid.Rows)
{
if (gdRow.RowType == DataControlRowType.DataRow)
{
var label1 = (Label) gdRow.Cells[1].FindControl("myLabel");
text1.Text = text1;
var label2 = (Label) gdRow.Cells[1].FindControl("myLabel");
text2.Text = text2;
var label3 = (Label) gdRow.Cells[1].FindControl("myLabel");
text3.Text = text3;
}
}
but it doesn't work, at the end all the labels contain text3
我的网格视图:
<asp:GridView ID="mygrid" runat="server" AutoGenerateColumns="False"
Visible="True" width="600px" onrowcreated="GdOrarRowCreated">
<Columns>
<asp:TemplateField >
<HeaderTemplate>
<asp:Label ID="text" runat="server" Text="text" ></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label runat="server" ID="myLabel" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="asd" Visible="True" HeaderText="col2"/>
<asp:BoundField DataField="dsa" Visible="True" HeaderText="col3"/>
<asp:BoundField DataField="qwe" Visible="True" HeaderText="col4"/>
<asp:BoundField DataField="ewq" Visible="True" HeaderText="col5"/>
</Columns>
</asp:GridView>