将HashSet绑定到DataGridView DataSource C#

时间:2015-09-06 20:41:57

标签: c# asp.net data-binding datagridview datasource

我是新手我试图仅将HashSet绑定为DataGridView的DataSource,但仍有GridView

中的任何项目
     dataGridViewBookList.DataSource = null;
     dataGridViewBookList.DataSource = bookContainer;

创建哈希集

   bookContainer = new HashSet<BookModel>();
   dataGridViewBookList.DataSource = bookContainer;

BookModel类

 class BookModel
    {
        public string Name { get; set; } 
        public uint Year { get; set; }
        public string Series { get; set; }
        public string Pulbisher { get; set; }
        public string Author { get; set; }
        public string Language { get; set; }
        public uint PagesCount { get; set; }
    }

请帮助将哈希集绑定到GridView。对不起,这个问题我是C#的新手 非常感谢你。

2 个答案:

答案 0 :(得分:0)

您可以尝试使用bookContainer的ToList()

答案 1 :(得分:0)

试试这个

 var book = new BookModel//fill it with your data
        {
            Author = "a",
            Language = "l",
            Name = "n",
            PagesCount = 0,
            Pulbisher = "p",
            Series = "s",
            Year = 1990
        };
        var list=new List<BookModel>();
        list.Add(book);
        var bookContainer = new HashSet<BookModel>(list);//you have to pass list to HashSet constructor

        dataGridViewBookList.DataSource = bookContainer.ToList();