winforms - Customized sorting on DataTable in C#? -


this c# winform question. have datagridview bounded datatable. construct datatable myself, several datacolumn instances. when datatable bound datagridview, default, every column sortable clicking headers of datagridview.

but sorting behavior "by default". seems sorted string. true if put code:

datacolumn dc = new datacolumn("myobjectcolumn", typeof(myobject));

and myobject has overriden tostring() , has implemented icomparable interface. means if have told datatable how sort special column implementation of icomparable interface, datagridview still doesn't way expect.

so how can let datatable sort data in way want?

thanks answers.

i recommend using defaultview of datatable. has built in sorting features little more extendable. easiest rowfilter, i'm not sure if you're looking if data types overridden .tostring() @ table level.

edit: added code snippet

a custom method maybe overrides or called during sort event of datagridview might able sort dataview before binding occurs. understand it, icomparable never gets called unless specify called.

protected void sortgrid() {     system.data.dataview dv = mydatatable.defaultview;      myojbect comparer = new myobject();     // comparer specifics go here. sort order, column/fieldname etc    // or custom properties used in sorting     dv.sort(comparer)     dgmygrid.datasource = dv    dgmygrid.databind()  } 

Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -