0

gridview 放置在转发器中,它在 itemdata 绑定上显示错误

代码,

  <asp:Repeater ID="rptSearchResult" runat="server" 
                OnItemDataBound="rptSearchResult_ItemDataBound">
          <HeaderTemplate>
         <table border="0">
             <tr>
                 <td> Cost Page</td>
                 <td> Cost Page Description</td>
                 <td> Vendor Name</td>
                 <td> Bill Type</td>
             </tr>
      </HeaderTemplate>

      <ItemTemplate>
         <tr>
            <td> <%# Eval("CostPage")%></td>
               <td><%# Eval("CostPageDescription")%> </td>
               <td> <%# Eval("VendorName")%> </td>
               <td><%# Eval("BillType")%> </td>
         </tr>
           <tr>
            <td>
               <asp:GridView ID="gvDetails" runat="server" >
                   <Columns>  
                     <asp:TemplateField HeaderText="">
                     <ItemTemplate> 
                         <asp:CheckBox ID="chkSelect" runat="server" />
                     </ItemTemplate>
                    </asp:TemplateField>              
                     <asp:BoundField DataField="ItemId" HeaderText="Item ID"/>                    
                     <asp:BoundField DataField="ItemDescription"  HeaderText="Item Description"/>
                     <asp:BoundField DataField="BrandCode"   HeaderText="Brand Code"/>                    

                    </Columns> 

               </asp:GridView>  </td>
         </tr>
      </ItemTemplate>

      <FooterTemplate>
         </table>
      </FooterTemplate>

        </asp:Repeater>

在 .cs 文件中,

    protected void rptSearchResult_ItemDataBound(
       object sender, RepeaterItemEventArgs e)
    {
        GridView gd = (GridView)e.Item.FindControl("gvDetails");


        details e1 = new details();
        e1.itemid= 1;
        e1.itemdesc = "item1";
        e1.brandcode = "BBB";

        List<details> employees = new List<details>();
        employees.Add(e1);

        gd.DataSource = employees;
        gd.DataBind();

}

4

2 回答 2

2

检查您的gd对象天气是否为空,而不是执行您的某些条件。因此代码应如下所示:

GridView gd = (GridView)e.Item.FindControl("gvDetails");

if(gd != null) {
  gd.DataSource = employees;
  gd.DataBind();
}

编辑: 错误即将到来,因为 of 中的名称DataField="ItemDescription"asp:BoundField您在数据绑定时指定的数据源员工属性不匹配。标记代码应该是

<asp:BoundField DataField="itemdesc"  HeaderText="Item Description"/>                
于 2013-07-04T16:49:30.490 回答
0

通过添加以下检查解决了该问题:

 if( (e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem)) 
  {  
    //Non-null value for grid
  }

现在,gridview 是非空值。

于 2013-07-05T13:18:20.983 回答