更改嵌套在Repeater中的Gridview的Rowdataboud中的标题文本

时间:2014-12-16 05:29:02

标签: c# asp.net gridview repeater

我有一个嵌套在转发器中的gridview,我想在行数据绑定上或通过

更改gridview列的标题文本
 <HeaderTemplate>
<asp:Label runat="server" ID="lblMode" Text='<%# Eval("IsValidForPromoCode")%>'>
</asp:Label></HeaderTemplate>

无论哪个方便。

.aspx page

<asp:Repeater ID="repRequest" runat="server" OnItemDataBound="repRequest_ItemDataBound">
                                <ItemTemplate>
                                    <table style="width: 100%; font-weight: bold;" cellpadding="5" cellspacing="0">
                                        <tr>
                                            <td colspan="12" align="right">
                                                <a id="aSetPreference" runat="server" href="#">Attached Document(s)-</a>
                                                <asp:Label ID="lblDocumentCount" CssClass="redFont" runat="server"></asp:Label>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="left" class="gray-bg" style="width: 8%;">
                                                <b>Request#:</b>
                                            </td>
                                            <td style="width: 100px;">
                                                <span class="detail-info-color">
                                                    <%# Eval("RequestNumber")%></span>
                                            </td>
                                            <td align="left" class="gray-bg" style="width: 5%;">
                                                <asp:Label ID="lblreqDetID" runat="server" Visible="false" Text='<%# Bind("TravelDetailsID") %>'></asp:Label>
                                                <b>Date:</b>
                                            </td>

                                            <td align="left" class="gray-bg" style="width: 5%;">
                                                <b>Class:</b>
                                            </td>
                                            <td>
                                                <span class="detail-info-color">
                                                    <%# Eval("Class")%></span>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td colspan="12">
                                                <asp:GridView ID="gvOption" CssClass="gridRow" runat="server" AutoGenerateColumns="False"
                                                    Width="100%" OnRowDataBound="gvOption_RowDataBound">
                                                    <Columns>
                                                        <asp:TemplateField HeaderText="#">
                                                            <ItemTemplate>
                                                                <%#Container.DataItemIndex+1 %>
                                                            </ItemTemplate>
                                                        </asp:TemplateField>
                                                        <asp:TemplateField HeaderText="Service Provider">
                                                            <ItemTemplate>
                                                                <asp:Label ID="txtNumber" runat="server" Width="80px" Text='<%# Bind("Number") %>'></asp:Label>
                                                            </ItemTemplate>
                                                        </asp:TemplateField>
                                                        <asp:TemplateField>
                                                            <HeaderTemplate>                                                                
                                                                <asp:Label runat="server" ID="lblMode" Text='<%# Eval("IsValidForPromoCode") %>'></asp:Label></HeaderTemplate>
                                                            <ItemTemplate>
                                                                <asp:Label ID="txtName" runat="server" Width="100px" Text='<%# Bind("Name") %>'></asp:Label>
                                                                <asp:Label ID="lblOptionID" Visible="false" runat="server" Width="100px" Text='<%# Bind("optionID") %>'></asp:Label>
                                                            </ItemTemplate>
                                                        </asp:TemplateField>                                                            
                                                    </Columns>
                                                </asp:GridView>
                                            </td>
                                        </tr>
                                    </table>
                                </ItemTemplate>
                            </asp:Repeater>

背后的代码

 protected void gvOption_RowDataBound(object sender, GridViewRowEventArgs e)
{
    try
    {
        GridView gvOption = (GridView)sender;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {               
            gvOption.Columns[1].HeaderText = "Last Name";               
        }
        if (e.Row.RowType == DataControlRowType.Header)
        {
            gvOption.Columns[2].HeaderText = "Last Name";
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

无论我采取哪种方式都没有影响。请提出我缺少的内容。

1 个答案:

答案 0 :(得分:1)

试试这个: -

if (e.Row.RowType == DataControlRowType.Header)
{
    e.Row.Cells[1].Text  = "Last Name";               
}

在这里,我已经硬编码了相应更改它所需的Cells值。

<强>更新: -
RowDataBound中找到控件: -

if (e.Row.RowType == DataControlRowType.DataRow)
{
    Label txtNumber = (Label)e.Row.FindControl("txtNumber");
    txtNumber.ForeColor = System.Drawing.Color.Red;
}