如何在网格视图中显示类列表 [英] How do I display list of class into grid view

查看:58
本文介绍了如何在网格视图中显示类列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有5个属性的学生课程,我正在从用户那里获取输入并保存到此List< student>。我通过会话将此列表传递给ViewDetails页面。到目前为止一切都很好。但我无法在网格视图中打印此值。请帮帮我。我的代码是:



公共部分类ViewDetails:System.Web.UI.Page

{

protected void Page_Load(object sender,EventArgs e)

{



List< student> obj =(List< student>)Session [PassValue];



GridView1.DataSource = obj;

GridView1.DataBind() ;

}



我们不能通过制作列表中的对象来做dis?



我尝试了什么:



我试图将列表转换为DataTable但我不能这样做。

解决方案

首先检查Session是否包含任何数据:使用调试器查看 obj 之后已被强制转换并检查它是否包含一个列表,并且该列表不为空。



如果它不是空的,那么看看你的 student class:它暴露了哪些属性?如果它只包含方法和字段,那么Gridview将无法显示。



如果所有这些看起来都正确,它应该可以工作 - 但你可以转换列表到数据表:将列表转换为DataTable [ ^ ]将向您展示如何

I have a class "Student" with 5 attributes, I am taking input from the user and saving to this List<student>. I am passing this list to "ViewDetails" page through session. Everything is fine till now. But I am unable to print the values of this in the grid view. Please help me.My code is:

public partial class ViewDetails : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

List<student> obj = (List<student>)Session["PassValue"];

GridView1.DataSource = obj;
GridView1.DataBind();
}

Can't we do like dis by making an object of the list?

What I have tried:

I tried to convert list into DataTable but I couldn't do that.

解决方案

Start by checking that the Session contains any data: use the debugger to look at the obj after it has been cast and check it contains a list, and that the list is not empty.

If it isn't empty, then look at your student class: what properties does it expose? If it only contains methods and fields, then the Gridview will have nothing to display.

If all of that looks right, it should work - but you can convert the list to a datatable: Converting a List to a DataTable[^] will show you how.


这篇关于如何在网格视图中显示类列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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