开发者

sorting datagridview programmatically being error

hi i have datagridview name data1, and binding data to data1 from database, when i click the column header for sorting the data,,some error show up..the error like this "DataGridView control must be bound to an IBindingList object to be sorted".

this is sample of the code..

SomeDataContext db = new SomeDataContext();

data1.DataSource = db.data.ToList();


private void data1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)   
{     
      dataGridView1.Sort(dataGridView1.Columns[e.ColumnIndex],                
      ListSortDire开发者_运维百科ction.Ascending);      
}

any solution guys??thanks in advance


you need to use the SortableBindingList class:

 SortableBindingList<person> persons = new SortableBindingList<person>();
 persons.Add(new Person(1, "timvw", new DateTime(1980, 04, 30)));
 persons.Add(new Person(2, "John Doe", DateTime.Now));

 this.dataGridView1.AutoGenerateColumns = false;
 this.ColumnId.DataPropertyName = "Id";
 this.ColumnName.DataPropertyName = "Name";
 this.ColumnBirthday.DataPropertyName = "Birthday";
 this.dataGridView1.DataSource = persons;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜