【问题标题】:i selected this method by objectdatasource in gridview , but the gridview appeared empty我在 gridview 中通过 objectdatasource 选择了此方法,但 gridview 显示为空
【发布时间】:2014-04-01 14:05:34
【问题描述】:

我在 gridview 中通过 objectdatasource 选择了这个方法,但是 gridview 显示为空 ![在此处输入图片说明][1]

[1]:gridview 照片.jpg

public List<Item> Item_Getall()
{
    List<Item> data = new List<Item>();
    SqlCommand cmd = new SqlCommand("c_get_all_item",oo.conn);
    cmd.CommandType = CommandType.StoredProcedure;
    oo.conn.Open();
    SqlDataReader rdr = cmd.ExecuteReader();
    while (rdr.Read())
    {
        data.Add(new Item());
        {
            Name_id = (rdr["item_name_id_pk"].ToString());
            Name_arabic = (rdr["item_name_arabic"].ToString());
            Component_english = (rdr["item_componant"].ToString());
            Component_arabic = (rdr["item_componant_arabic"].ToString());
            Price = float.Parse(rdr["item_price"].ToString());
            Image = (rdr["item_image"].ToString());
            Category = (rdr["item_category_name_id_fk"].ToString());
        }
    }
    oo.conn.Close();
    return data;
}

【问题讨论】:

  • 试试data.Add(new Item(){/*Your asignment code*/});
  • 是的,就是这样。上面列出的代码实际上不会编译,但我假设他的代码不同,因为他说 gridview 不显示。

标签: c# asp.net gridview


【解决方案1】:

更新 - 尝试让我的回答更有帮助

如果您想简单地将数据绑定到 gridview,在后端很容易做到。假设您在 Page_Load 方法中执行此操作。

protected void Page_Load(object sender, eventArgs e){

    var data = Item_Getall(); //Creates a list of items by calling your method.

    gridView.DataSource = data; //assuming the ID of your gridview is "gridView". It might be something different.

    gridView.DataBind(); //binds the data to your grid. If you have it set to auto populate, it should essentially list out every public property for each object in the list.

}

【讨论】:

  • 数据是什么意思? ..你的意思是列表名称吗?
  • 用更多细节更新了我的答案
  • 我遇到错误 DataSource 和 DataSourceID 都在“GridView_show_items”上定义。删除一个定义。
猜你喜欢
  • 2013-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-23
  • 1970-01-01
相关资源
最近更新 更多