我在GridView中的ObjectDataSource通过选择此方法,但在GridView出现空洞 [英] i selected this method by objectdatasource in gridview , but the gridview appeared empty

查看:91
本文介绍了我在GridView中的ObjectDataSource通过选择此方法,但在GridView出现空洞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用的ObjectDataSource在GridView中选择此方法,但在GridView出现空洞
![在这里输入的形象描述] [1]

[1]:GridView的photo.jpg

 公开名单<项目> Item_Getall()
{
    清单<项目>数据=新的List<项目>();
    CMD的SqlCommand =新的SqlCommand(c_get_all_item,oo.conn);
    cmd.CommandType = CommandType.StoredProcedure;
    oo.conn.Open();
    SqlDataReader的RDR = cmd.ExecuteReader();
    而(rdr.Read())
    {
        data.Add(新项目());
        {
            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());
            价格= float.Parse(RDR [ITEM_PRICE]的ToString());
            图像=(RDR [item_image]的ToString());
            分类=(RDR [item_category_name_id_fk]的ToString());
        }
    }
    oo.conn.Close();
    返回的数据;
}


解决方案

更新 - 尽量让我的答案更有帮助

如果你想简单的数据绑定到一个GridView,这是很容易做到这一点的后端。比方说,你在Page_Load方法做到这一点。

 保护无效的Page_Load(对象发件人,EventArgs的发送){    VAR数据= Item_Getall(); //通过调用你的方法创建项目的列表。    gridView.DataSource =数据; //假设你的GridView的ID是GridView的。这可能是不同的东西。    gridView.DataBind(); //将数据绑定到你的网格。如果你把它设置为自动填充,它基本上应该列出对列表中的每个对象的每个公共财产。}

i selected this method by objectdatasource in gridview , but the gridview appeared empty ![enter image description here][1]

[1]:gridview photo.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;
}

解决方案

UPDATE - Try to make my answer more helpful

If you want to simply bind your data to a gridview, it is very easy to do it on the backend. Lets say you do it in the Page_Load method.

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.

}

这篇关于我在GridView中的ObjectDataSource通过选择此方法,但在GridView出现空洞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆