GridView绑定ArraryList和 HashTable
场景: 在一个项目中,需要将用户添加的数据,在写入数据库前,先用某一种数据结构暂存一下,并用GridView显示出来,最容易想到的是用ArrayList和HashTable,将ArrayList或HashTable绑定到DropDownList之类的列表控件没有什么问题,问题是如何绑定到GridView上去?
将ArrayList绑定到GridView是可以的,具体的实现请参考下述代码:
public class Info//需要用到这个类的实例来给ArrayList添加内容!!
{
public string Name;
public int VoteCount;
}
///假设我们已经有sqldatareader对象dr,并且已经获取的数据了
ArrayList myList = new ArrayList();//创建ArranList
while (dr.Read())
{
Info myinfo = new Info();//创建类Info的实例
myinfo.Name = dr["Name"].ToString();
myinfo.VoteCount = Int32.Parse(dr["VoteCount"].ToString());
myList.Add(myinfo);//添加Arranlist
}
dr.Close();
mygridView.DataSource = myList;//arrayList绑定到Gridview
mygridView.DataBind();
.aspx文件中绑定数据如下:
<%# ((Info)Container.DataItem).VoteCount %>
<%# ((Info)Container.DataItem).Name %>
将ArrayList绑定到GridView是可以的,具体的实现请参考下述代码:
public class Info//需要用到这个类的实例来给ArrayList添加内容!!
{
public string Name;
public int VoteCount;
}
///假设我们已经有sqldatareader对象dr,并且已经获取的数据了
ArrayList myList = new ArrayList();//创建ArranList
while (dr.Read())
{
Info myinfo = new Info();//创建类Info的实例
myinfo.Name = dr["Name"].ToString();
myinfo.VoteCount = Int32.Parse(dr["VoteCount"].ToString());
myList.Add(myinfo);//添加Arranlist
}
dr.Close();
mygridView.DataSource = myList;//arrayList绑定到Gridview
mygridView.DataBind();
.aspx文件中绑定数据如下:
<%# ((Info)Container.DataItem).VoteCount %>
<%# ((Info)Container.DataItem).Name %>
学习,积累中......