Selalu_Ingin_Belajar
Selalu_Ingin_Belajar

Reputation: 375

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],                
      ListSortDirection.Ascending);      
}

any solution guys??thanks in advance

Upvotes: 4

Views: 7116

Answers (1)

Joe
Joe

Reputation: 82654

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;

Upvotes: 5

Related Questions