我在 GridView 中通过对象数据源选择了此方法,但网格视图显示为空
本文关键字:网格 视图 显示 此方法 GridView 对象 选择 数据源 我在 | 更新日期: 2023-09-27 17:56:01
>我在 gridview 中通过 objectdatasource 选择了此方法,但网格视图显示为空![在此输入图像描述][1]
[1]:网格视图照片.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;
}
更新 - 尝试让我的答案更有帮助
如果只想将数据绑定到 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.
}