在此上下文中无法访问System.Web.UI.WebControls.GridViewRow',因为它是'Friend'

时间:2013-08-27 04:21:23

标签: asp.net gridview rowdatabound

我想将Gridview中的第一列设置为零,但是这样做会导致以下错误:在此上下文中无法访问System.Web.UI.WebControls.GridViewRow'因为它是'Friend'

这是我的网格:

 <asp:GridView ID="gvPurchaseOrderNum" runat="server" AutoGenerateColumns="False"
                        onrowdeleting="gvPurchaseOrderNum_RowDeleting" onrowediting="gvPurchaseOrderNum_RowEditing"
                        onrowupdating="gvPurchaseOrderNum_RowUpdating" onrowcommand="gvPurchaseOrderNum_RowCommand"
                        ShowFooter="True" Width="482px" 
                        onrowcancelingedit="gvPurchaseOrderNum_RowCancelingEdit">
                        <Columns>
                             <asp:TemplateField HeaderText="Purchase Order ID" Visible="true">
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtPonumberID" runat="server" Text='<%# Bind("ponumberID") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtPonumberIDInsert" runat="server"></asp:TextBox>
                                </FooterTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblPonumberID" runat="server" Text='<%# Bind("PonumberID") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                             <asp:TemplateField HeaderText="Purchase Order Number">
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtPonumber" runat="server" Text='<%# Bind("poNumber") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtPoNumberInsert" runat="server"></asp:TextBox>
                                   <%-- <asp:RequiredFieldValidator runat="server" ID="reqtxtPoNumberInsert" ControlToValidate="txtPoNumberInsert" ErrorMessage="Required Field" Display="Dynamic" />--%>
                                </FooterTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblPoNumber" runat="server" Text='<%# Bind("PoNumber") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField ShowHeader="False">
                                <EditItemTemplate>
                                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True"
                                        CommandName="Update" Text="Update"></asp:LinkButton>
                                    &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
                                        CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:LinkButton ID="btnInsert" runat="server" CommandName="insertXMLData">Insert</asp:LinkButton>
                                </FooterTemplate>
                                <ItemTemplate>
                                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
                                        CommandName="Edit" Text="Edit"></asp:LinkButton>
                                </ItemTemplate>
                                <ItemStyle Width="120px" />
                            </asp:TemplateField>
                            <asp:CommandField ShowDeleteButton="True" />
                         </Columns>
                         <emptydatatemplate>
                          <b>Enter Purchase Order Number(s)</b> <br /> 
                             <asp:TextBox ID="txtStartpoNumID" runat="server"></asp:TextBox><br />
                             <asp:TextBox ID="txtStartpoNum" runat="server"></asp:TextBox><br />
                             <%-- <asp:RequiredFieldValidator runat="server" ID="reqtxtStartpoNum" ControlToValidate="txtStartpoNum" ErrorMessage="Required Field" Display="Dynamic" />--%>
                             <asp:LinkButton ID="lnkpro" runat="server" OnClick="writeStartpoNum"  Text="Add Part Description"></asp:LinkButton> 
                            <p>&nbsp;</p>
                        </emptydatatemplate> 
                    </asp:GridView> 

以下是我正在实施任务的代码:

Protected Sub gvPurchaseOrderNum_RowDataBound(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            ' set the first column to zero
            e.Row.Cells(0).Text = 0
        End If


    End Sub

......请问我能在这里做错什么吗?我用Google搜索了错误,但无法找到解决问题的方法。

1 个答案:

答案 0 :(得分:0)

使用FindControl()方法代替Cells集合,如下所示:

Protected Sub gvPurchaseOrderNum_RowDataBound(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        ' set the first column to zero
        Dim theLabel As Label = e.Row.FindControl("lblPonumberID")
        If theLabel Is Not Nothing Then
            theLabel.Text = "0"
        End If
    End If
End Sub

由于TemplateField中可能有多个控件,因此使用FindControl()方法查找要更改Text属性的确切项目会更容易取决于通过索引获得正确的单元格。