0

我在我的 DataBind 命令上收到此错误我正在尝试使用 asp 和 c# 从列表中填充网格视图。场景是中继器填充侧边栏上的类别链接,基于选择将填充主 div 中的网格视图。当我使用数据集作为数据源时它运行良好,但在这种情况下我使用的是泛型,我不知道错误来自哪里。列表被填充只是不能数据绑定它

public partial class Products : System.Web.UI.Page
{
    string ItemNumber;
    string ItemDescription;
    string  PrePrice;
    string Size;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PopCategories();

        }
    }  
private List<Products> GetCategoryDetails(string HiddenField1)
    {
        DataTable table = new DataTable();
        table = new BizLayer().grabdata(HiddenField1);

        List<Products> list = new List<Products>();
        foreach (DataRow dr in table.Rows)
        {
            Products products = new Products();
            products.ItemNumber = dr["Item #"].ToString();
            products.ItemDescription = dr["Item Description"].ToString();
            products.PrePrice = dr["Pre Price"].ToString();
            products.Size = dr["Size"].ToString();
            list.Add(products);
        }
        return list;
    }
 private void PopDetails()
    {
        DetailsGridView.DataSource = GetCategoryDetails(HiddenField1.Value);

此处出错 DetailsGridView.DataBind();

    }  

  protected void Repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
    {       
        //Get Command Arguement from repeater OnItemCommand
        if (e.CommandName == "cmd")
        {
            HiddenField1.Value = (e.CommandArgument).ToString();             

             GetCategoryDetails(HiddenField1.Value);
             PopDetails();
        }            
    }

    protected void DetailsGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {

            DetailsGridView.PageIndex = e.NewPageIndex;
            //  DataSource  
            DetailsGridView.DataSource = GetCategoryDetails(HiddenField1.Value);

            DetailsGridView.DataBind();            
    }

 <asp:GridView ID="DetailsGridView" AllowPaging="true" PageSize="3" CssClass="dataGrid" 
  RowStyle-  CssClass="chartItemStyle" AlternatingRowStyle-CssClass="chartAlternatingItemStyle" 
  HeaderStyle-  CssClass="chartHeaderStyle" runat="server"   OnPageIndexChanging="DetailsGridView_PageIndexChanging">   </asp:GridView>
4

1 回答 1

0
public partial class Products : System.Web.UI.Page
{
   public class Products2
   {
       public string ItemNumber { get; set; }
       public string ItemDescription { get; set; }
       public string PrePrice { get; set; }
       public string Size { get; set; }
   }
//....

然后:

List<Products2> list = new List<Products2>();
foreach (DataRow dr in table.Rows)
{
     Products2 products = new Products2();
     products.ItemNumber = dr["Item #"].ToString();
     products.ItemDescription = dr["Item Description"].ToString();
     products.PrePrice = dr["Pre Price"].ToString();
     products.Size = dr["Size"].ToString();
     list.Add(products);
}
于 2013-08-13T20:51:07.760 回答