现在我有一个带有以下代码的Gridview;
<asp:GridView CssClass="grdInspStra" ID="grdInspStra" runat="server" AutoGenerateColumns="false" OnRowCreated="grdInspStra_RowCreated">
<AlternatingRowStyle CssClass="grdItemEven" />
<Columns>
<asp:TemplateField HeaderText="Zone Code" ItemStyle-CssClass="grdItemCenter">
<ItemTemplate>
<asp:Label ID="lblZone" runat="server" Text='<%# Eval("ZoneCode")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Group Code" ItemStyle-CssClass="grdItemCenter">
<ItemTemplate>
<asp:Label ID="lblGroup" runat="server" Text='<%# Eval("GroupCode")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Component Code" ItemStyle-CssClass="grdItemCenter">
<ItemTemplate>
<asp:Label ID="lblComp" runat="server" Text='<%# Eval("ComponentCode")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Component Tag No." ItemStyle-CssClass="grdItemCenter">
<ItemTemplate>
<asp:Label ID="lblCompTagNo" runat="server" Text='<%# Eval("ComponentTagRef")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Risk Category" ItemStyle-CssClass="grdItemCenter">
<ItemTemplate>
<asp:Label ID="lblRiskCat" runat="server" Text='<%# Eval("RiskCategory")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Proposed Interval" ItemStyle-CssClass="grdItemCenter">
<ItemTemplate>
<asp:Label ID="lblInterval" runat="server" Text='<%# Eval("ProposedInterval")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Latest Date" ItemStyle-CssClass="grdItemCenter">
<ItemTemplate>
<asp:Label ID="lblDateLatest" runat="server" Text='<%# Eval("DateLI")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="H1" ItemStyle-CssClass="grdItemCenter">
<ItemTemplate>
<asp:Label ID="lblFirstHalf0" runat="server" Text='<%# Eval("FirstHalf0")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="H2" ItemStyle-CssClass="grdItemCenter">
<ItemTemplate>
<asp:Label ID="lblSecondHalf0" runat="server" Text='<%# Eval("SecondHalf0")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
但是我试图消除这段代码;
<asp:TemplateField HeaderText="H1" ItemStyle-CssClass="grdItemCenter">
<ItemTemplate>
<asp:Label ID="lblFirstHalf0" runat="server" Text='<%# Eval("FirstHalf0")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="H2" ItemStyle-CssClass="grdItemCenter">
<ItemTemplate>
<asp:Label ID="lblSecondHalf0" runat="server" Text='<%# Eval("SecondHalf0")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
这是因为名为H1
和H2
的列需要从0 =&gt;重复。 30,以便在代码末尾标签名称变为;
<asp:Label ID="lblFirstHalf30" runat="server" Text='<%# Eval("FirstHalf30")%>'></asp:Label>
<asp:Label ID="lblSecondHal30" runat="server" Text='<%# Eval("SecondHalf30")%>'></asp:Label>
我设法使用Page_Load;
创建列TemplateField tfield;
for (int i = 0; i < 31; i++)
{
tfield = new TemplateField();
tfield.HeaderText = "H1";
grdInspStra.Columns.Add(tfield);
tfield = new TemplateField();
tfield.HeaderText = "H2";
grdInspStra.Columns.Add(tfield);
}
但是,我不知道如何使用C#放置标签并从SQL服务器绑定数据。
我尝试从How to add TemplateField programmatically向我们举例,但我得到的是错误的值,因为它只是不循环。 (注意我只测试将数据从服务器放到H2
)
我尝试的是什么;
TemplateField tfield;
for (int i = 0; i < 31; i++)
{
tfield = new TemplateField();
tfield.HeaderText = "H1";
tfield.ItemStyle.CssClass = "grdItemCenter";
grdInspStra.Columns.Add(tfield);
tfield = new TemplateField();
tfield.HeaderText = "H2";
tfield.ItemStyle.CssClass = "grdItemCenter";
tfield.ItemTemplate = new LabelColumn();
grdInspStra.Columns.Add(tfield);
}
class LabelColumn : ITemplate
{
public void InstantiateIn(Control container)
{
Label label = new Label();
label.ID = "lblSecondHalf";
label.DataBinding += new EventHandler(label_DataBinding);
container.Controls.Add(label);
}
private void label_DataBinding(Object sender, EventArgs e)
{
Label label = (Label)sender;
GridViewRow row = (GridViewRow)label.NamingContainer;
label.Text = DataBinder.Eval(row.DataItem, "SecondHalf0").ToString();
}
}
有人可以帮我解决这个问题吗?
答案 0 :(得分:0)
下午休息后,我设法解决了这个问题。
在class LabelColumn : ITemplate
中添加int i
,示例;
class LabelColumn : ITemplate
{
int i;
public LabelColumn(int i)
{
this.i = i;
}
public void InstantiateIn(Control container)
{
Label label = new Label();
label.ID = "lblSecondHalf" + i;
label.DataBinding += new EventHandler(label_DataBinding);
container.Controls.Add(label);
}
private void label_DataBinding(Object sender, EventArgs e)
{
Label label = (Label)sender;
GridViewRow row = (GridViewRow)label.NamingContainer;
label.Text = DataBinder.Eval(row.DataItem, "SecondHalf" + i).ToString();
}
}