我要綁定從數據表如何從數據表
網格視圖中添加文本標籤裏面的GridViewvar 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();
在我的網格
第一列包含標籤
我想從數據表第一列添加第一列DVIEW
我想是這樣的:
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
我的GridView:
<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>
請發表您的GridView標記 –