Since nobody wanted to answer this, I thought I'd answer it.
The problem is in the custom binding source. Auto Sort will not work when custom binding source is used.
So you need to remove the line:
datagridview1.Sort(datagridview1.Columns["Number"], sortDirection);
Instead after you fetch data apply sorting logic like this:
private IEnumerable<patient> GetSortedModel(IModel<patient> model, int? sortColumnIndex, ListSortDirection? sortDirection)
{
if (sortColumnIndex != null)
{
if (sortDirection == ListSortDirection.Ascending)
{
switch (sortColumnIndex)
{
case 0:
return model.GetList().OrderBy(x => x.FacilityName).ThenBy(x => x.Name);
case 1:
return model.GetList().OrderBy(x => x.ID).ThenBy(x => x.Name);
case 2:
return model.GetList().OrderBy(x => x.Name).ThenBy(x =>x.Name);
....
}
}
else
{
switch (sortColumnIndex)
{
case 0:
return model.GetList().OrderByDescending(x => x.FacilityName).ThenBy(x => x.Name);
case 1:
return model.GetList().OrderByDescending(x => x.ID).ThenBy(x => x.Name);
case 2:
return model.GetList().OrderByDescending(x => x.Name).ThenBy(x => x.Name);
....
}
}
}
return model.GetList().OrderBy(x => x.Name);
}</patient></patient>
And then apply the datasource.
datagridview1.source = new BindingSource { DataSource = model};