How do i get my datagridview to sort?

C

Claire

Hi,
Nothing happens when I click on the datagridview column header. I expect it
to sort.
What am i missing please?

the datagridview's datasource is linked to the datagridviews columns to show
"ResourceID", "EnglishText" and "Translated" which are public fields of my
StringResource class. Each column sort is set to autosort.

_datasource is a bindingsource control dropped onto the form but its
contents are set up dynamically.

private System.Windows.Forms.BindingSource _dataSource;

// Clear datasource
_dataSource.Clear();
// Add records to the datasource,
ResXResourceReader reader = new
ResXResourceReader(EnglishItem.FileNamePath);
try
{
IDictionaryEnumerator enumerator = reader.GetEnumerator();
foreach (DictionaryEntry de in reader)
{
StringResource res = new StringResource((string)de.Key,
(string)de.Value,"");
_dataSource.Add(res);
}
}
finally
{
reader.Close();
}
 
M

Marc Gravell

The key thing here is that the DataGridView isn't responsible for sorting;
the underlying data-source (i.e. .DataSource of the DataSource) is.

So: what is the data-source? I have posted (to this forum) several examples
of sortable BindingList<T> implementations that I might be able to dig out?
 
C

Claire

tbh I think i have done something I shouldnt have done as Im a noob and it
appears to work.

1) I dropped a binding source on my form, then dropped a bindingnavigator
and linked the two together in the property editor.
private System.Windows.Forms.BindingSource _dataSource;
private System.Windows.Forms.BindingNavigator Navigator;

2) I created my StringResource class
internal class StringResource
{
private string _stringID = "";
private string _englishOriginal = "";
private string _translation = "";
public string StringID
{
get { return _stringID; }
set { _stringID = value; }
}
public string EnglishOriginal
{
get {return _englishOriginal;}
set{_englishOriginal = value;}
}
public string Translation
{
get { return _translation; }
set { _translation = value; }
}
}

3) I dropped a datagridview on the form and manually created 3 columns. I
then typed in DataPropertyName to link those to the associated properties of
the StringResource class.The following text is what vis studio autogenerated
for my "StringID" datagridview column. I set datagridview.DataSource
property in the designer to my BindingSource

this.ResourceID.DataPropertyName = "StringID";
this.ResourceID.HeaderText = "Resource ID";
this.ResourceID.Name = "ResourceID";
this.ResourceID.ReadOnly = true;
this.ResourceID.SortMode =
System.Windows.Forms.DataGridViewColumnSortMode.Automatic;

4) At run time I added new StringResource objects to the BindingSource as
follows
_dataSource.Clear();
foreach(blah blah)
{
StringResource res = new StringResource((string)de.Key, (string)de.Value);
_dataSource.Add(res);
}

So.... _dataSource.DataSource is (none)
Ive not generated any other objects eg DataView, DataTable etc etc
 
M

Marc Gravell

First - thankyou for the clear question; you posted enough to reproduce the
issue, without posting the entire app - much appreciated.

Using your code, I added my AdvancedList<T> from here:
http://groups.google.co.uk/group/microsoft.public.dotnet.languages.csharp/msg/2b7528c689f9ef84

This simply inherits from BindingList<T> and provides sort capabilities.

To attach it, to your grid all you need is something like:

AdvancedList<StringResource> list = new
AdvancedList<StringResource>();
bindingSource1.DataSource = list;

Having done that, I can now sort my DataGridView using the column headers.

Let me know if that works?

Marc
 
I

Ivan Farkas

Hi Marc,

Thank you for your code. It works great!
I added constructors that enables adding a list.
This enables me to sort a list with LinQ and add it to AdvancedList.

public AdvancedList() : base()
{
}

public AdvancedList(IList<T> list) : base(list)
{
}

Usage:
AdvancedList<MailDisp> _mailDispList = new AdvancedList<MailDisp>();
_mailDispList.Clear();

//Populate _mailDispList

// Sort by Date
_mailDispList = new AdvancedList<MailDisp>(_mailDispList.OrderByDescending(m => m.Date).ToList());

dgvMails.DataSource = _mailDispList;

Ivan



Marc Gravell wrote:

First - thankyou for the clear question; you posted enough to reproduce the
06-Mar-08

First - thankyou for the clear question; you posted enough to reproduce the
issue, without posting the entire app - much appreciated

Using your code, I added my AdvancedList<T> from here
http://groups.google.co.uk/group/microsoft.public.dotnet.languages.csharp/msg/2b7528c689f9ef8

This simply inherits from BindingList<T> and provides sort capabilities

To attach it, to your grid all you need is something like

AdvancedList<StringResource> list = new
AdvancedList<StringResource>()
bindingSource1.DataSource = list

Having done that, I can now sort my DataGridView using the column headers

Let me know if that works

Marc

Previous Posts In This Thread:

How do i get my datagridview to sort?
Hi
Nothing happens when I click on the datagridview column header. I expect it
to sort
What am i missing please

the datagridview's datasource is linked to the datagridviews columns to show
"ResourceID", "EnglishText" and "Translated" which are public fields of my
StringResource class. Each column sort is set to autosort

_datasource is a bindingsource control dropped onto the form but its
contents are set up dynamically

private System.Windows.Forms.BindingSource _dataSource

// Clear datasourc
_dataSource.Clear()
// Add records to the datasource
ResXResourceReader reader = new
ResXResourceReader(EnglishItem.FileNamePath)
tr

IDictionaryEnumerator enumerator = reader.GetEnumerator()
foreach (DictionaryEntry de in reader

StringResource res = new StringResource((string)de.Key,
(string)de.Value,"")
_dataSource.Add(res)


finall

reader.Close()
}

The key thing here is that the DataGridView isn't responsible for sorting; the
The key thing here is that the DataGridView isn't responsible for sorting;
the underlying data-source (i.e. .DataSource of the DataSource) is

So: what is the data-source? I have posted (to this forum) several examples
of sortable BindingList<T> implementations that I might be able to dig out?

tbh I think i have done something I shouldnt have done as Im a noob and it
tbh I think i have done something I shouldnt have done as Im a noob and it
appears to work

1) I dropped a binding source on my form, then dropped a bindingnavigator
and linked the two together in the property editor
private System.Windows.Forms.BindingSource _dataSource
private System.Windows.Forms.BindingNavigator Navigator

2) I created my StringResource clas
internal class StringResourc

private string _stringID = ""
private string _englishOriginal = ""
private string _translation = ""
public string StringI

get { return _stringID;
set { _stringID = value;

public string EnglishOrigina

get {return _englishOriginal;
set{_englishOriginal = value;

public string Translatio

get { return _translation;
set { _translation = value;



3) I dropped a datagridview on the form and manually created 3 columns. I
then typed in DataPropertyName to link those to the associated properties of
the StringResource class.The following text is what vis studio autogenerated
for my "StringID" datagridview column. I set datagridview.DataSource
property in the designer to my BindingSourc

this.ResourceID.DataPropertyName = "StringID"
this.ResourceID.HeaderText = "Resource ID";
this.ResourceID.Name = "ResourceID";
this.ResourceID.ReadOnly = true;
this.ResourceID.SortMode =
System.Windows.Forms.DataGridViewColumnSortMode.Automatic;

4) At run time I added new StringResource objects to the BindingSource as
follows
_dataSource.Clear();
foreach(blah blah)
{
StringResource res = new StringResource((string)de.Key, (string)de.Value);
_dataSource.Add(res);
}

So.... _dataSource.DataSource is (none)
Ive not generated any other objects eg DataView, DataTable etc etc

First - thankyou for the clear question; you posted enough to reproduce the
First - thankyou for the clear question; you posted enough to reproduce the
issue, without posting the entire app - much appreciated.

Using your code, I added my AdvancedList<T> from here:
http://groups.google.co.uk/group/microsoft.public.dotnet.languages.csharp/msg/2b7528c689f9ef84

This simply inherits from BindingList<T> and provides sort capabilities.

To attach it, to your grid all you need is something like:

AdvancedList<StringResource> list = new
AdvancedList<StringResource>();
bindingSource1.DataSource = list;

Having done that, I can now sort my DataGridView using the column headers.

Let me know if that works?

Marc

Re: How do i get my datagridview to sort?
thank you marc :)

You're welcome; I hope it worked!Marc
You're welcome; I hope it worked!

Marc


Submitted via EggHeadCafe - Software Developer Portal of Choice
Crypto Obfuscator for .NET - Product Review
http://www.eggheadcafe.com/tutorial...f8-f5fd987fafb1/crypto-obfuscator-for-ne.aspx
 
H

hal21th hal21th

Dear all,

I modified AdvancedList<T> a little bit, so that it can toggle order by ascending and descending.
It's ugly but works. Hope it can help.


public class AdvancedList<T> : BindingList<T>, IBindingListView
{
PropertyComparerCollection<T> _sorts;
PropertyDescriptor _lastProperty;
ListSortDirection _lastDirection;

protected override bool IsSortedCore
{
get
{
//return base.IsSortedCore;
return _sorts != null;
}
}

protected override void RemoveSortCore()
{
//base.RemoveSortCore();
_sorts = null;
}

protected override bool SupportsSortingCore
{
get
{
//return base.SupportsSortingCore;
return true;
}
}

protected override ListSortDirection SortDirectionCore
{
get
{
//return base.SortDirectionCore;
return _sorts == null ? ListSortDirection.Ascending : _sorts.PrimaryDirection;
}
}

protected override PropertyDescriptor SortPropertyCore
{
get
{
//return base.SortPropertyCore;
return _sorts == null ? null : _sorts.PrimaryProperty;
}
}

protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
{
if (prop.Equals(_lastProperty))
{
direction = _lastDirection == ListSortDirection.Ascending ?
ListSortDirection.Descending : ListSortDirection.Ascending;
_lastDirection = direction;
}
else
{
_lastProperty = prop;
_lastDirection = direction;
}
//base.ApplySortCore(prop, direction);
ListSortDescription[] arr = {
new ListSortDescription(prop, direction)
};
ApplySort(new ListSortDescriptionCollection(arr));
}

public void ApplySort(ListSortDescriptionCollection sortCollection)
{
bool oldRaise = RaiseListChangedEvents;
RaiseListChangedEvents = false;
try
{
PropertyComparerCollection<T> tmp
= new PropertyComparerCollection<T>(sortCollection);
List<T> items = new List<T>(this);
items.Sort(tmp);
int index = 0;
foreach (T item in items)
{
SetItem(index++, item);
}
_sorts = tmp;
}
finally
{
RaiseListChangedEvents = oldRaise;
ResetBindings();
}
}

String IBindingListView.Filter
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}

void IBindingListView.RemoveFilter()
{
throw new NotImplementedException();
}

ListSortDescriptionCollection IBindingListView.SortDescriptions
{
get { return _sorts.Sorts; }
}

bool IBindingListView.SupportsAdvancedSorting
{
get { return true; }
}

bool IBindingListView.SupportsFiltering
{
get { return false; }
}
}

public class PropertyComparerCollection<T> : IComparer<T>
{
private readonly ListSortDescriptionCollection _sorts;
private readonly PropertyComparer<T>[] _comparers;

public ListSortDescriptionCollection Sorts
{
get { return _sorts; }
}

public PropertyDescriptor PrimaryProperty
{
get
{
return _comparers.Length == 0 ? null : _comparers[0].Property;
}
}

public ListSortDirection PrimaryDirection
{
get
{
return _comparers.Length == 0 ? ListSortDirection.Ascending
: _comparers[0].Descending ?
ListSortDirection.Descending : ListSortDirection.Ascending; ;
}
}

public PropertyComparerCollection(ListSortDescriptionCollection sorts)
{
if (sorts == null)
throw new ArgumentNullException("sorts");
_sorts = sorts;
List<PropertyComparer<T>> list = new List<PropertyComparer<T>>();
foreach (ListSortDescription item in _sorts)
{
list.Add(new PropertyComparer<T>(item.PropertyDescriptor,
item.SortDirection == ListSortDirection.Descending));
_comparers = list.ToArray();
}
}

int IComparer<T>.Compare(T x, T y)
{
int result = 0;
for (int i = 0; i < _comparers.Length; i++)
{
result = _comparers.Compare(x, y);
if (result != 0) break;
}
return result;
}
}

public class PropertyComparer<T> : IComparer<T>
{
private readonly bool _descending;
private readonly PropertyDescriptor _property;

public bool Descending
{
get { return _descending; }
}

public PropertyDescriptor Property
{
get { return _property; }
}

public PropertyComparer(PropertyDescriptor property, bool descending)
{
if (property == null) throw new ArgumentNullException("property");
this._descending = descending;
this._property = property;
}

public int Compare(T x, T y)
{
int value = Comparer.Default.Compare(_property.GetValue(x), _property.GetValue(y));
return _descending ? -value : value;
}
}
 
H

hal21th hal21th

Dear all,

I modified AdvancedList<T> a little bit, so that it can toggle between ascending and descending.


public class AdvancedList<T> : BindingList<T>, IBindingListView
{
PropertyComparerCollection<T> _sorts;
PropertyDescriptor _lastProperty;
ListSortDirection _lastDirection;

protected override bool IsSortedCore
{
get
{
//return base.IsSortedCore;
return _sorts != null;
}
}

protected override void RemoveSortCore()
{
//base.RemoveSortCore();
_sorts = null;
}

protected override bool SupportsSortingCore
{
get
{
//return base.SupportsSortingCore;
return true;
}
}

protected override ListSortDirection SortDirectionCore
{
get
{
//return base.SortDirectionCore;
return _sorts == null ? ListSortDirection.Ascending : _sorts.PrimaryDirection;
}
}

protected override PropertyDescriptor SortPropertyCore
{
get
{
//return base.SortPropertyCore;
return _sorts == null ? null : _sorts.PrimaryProperty;
}
}

protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
{
if (prop.Equals(_lastProperty))
{
direction = _lastDirection == ListSortDirection.Ascending ?
ListSortDirection.Descending : ListSortDirection.Ascending;
_lastDirection = direction;
}
else
{
_lastProperty = prop;
_lastDirection = direction;
}
//base.ApplySortCore(prop, direction);
ListSortDescription[] arr = {
new ListSortDescription(prop, direction)
};
ApplySort(new ListSortDescriptionCollection(arr));
}

public void ApplySort(ListSortDescriptionCollection sortCollection)
{
bool oldRaise = RaiseListChangedEvents;
RaiseListChangedEvents = false;
try
{
PropertyComparerCollection<T> tmp
= new PropertyComparerCollection<T>(sortCollection);
List<T> items = new List<T>(this);
items.Sort(tmp);
int index = 0;
foreach (T item in items)
{
SetItem(index++, item);
}
_sorts = tmp;
}
finally
{
RaiseListChangedEvents = oldRaise;
ResetBindings();
}
}

String IBindingListView.Filter
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}

void IBindingListView.RemoveFilter()
{
throw new NotImplementedException();
}

ListSortDescriptionCollection IBindingListView.SortDescriptions
{
get { return _sorts.Sorts; }
}

bool IBindingListView.SupportsAdvancedSorting
{
get { return true; }
}

bool IBindingListView.SupportsFiltering
{
get { return false; }
}
}

public class PropertyComparerCollection<T> : IComparer<T>
{
private readonly ListSortDescriptionCollection _sorts;
private readonly PropertyComparer<T>[] _comparers;

public ListSortDescriptionCollection Sorts
{
get { return _sorts; }
}

public PropertyDescriptor PrimaryProperty
{
get
{
return _comparers.Length == 0 ? null : _comparers[0].Property;
}
}

public ListSortDirection PrimaryDirection
{
get
{
return _comparers.Length == 0 ? ListSortDirection.Ascending
: _comparers[0].Descending ?
ListSortDirection.Descending : ListSortDirection.Ascending; ;
}
}

public PropertyComparerCollection(ListSortDescriptionCollection sorts)
{
if (sorts == null)
throw new ArgumentNullException("sorts");
_sorts = sorts;
List<PropertyComparer<T>> list = new List<PropertyComparer<T>>();
foreach (ListSortDescription item in _sorts)
{
list.Add(new PropertyComparer<T>(item.PropertyDescriptor,
item.SortDirection == ListSortDirection.Descending));
_comparers = list.ToArray();
}
}

int IComparer<T>.Compare(T x, T y)
{
int result = 0;
for (int i = 0; i < _comparers.Length; i++)
{
result = _comparers.Compare(x, y);
if (result != 0) break;
}
return result;
}
}

public class PropertyComparer<T> : IComparer<T>
{
private readonly bool _descending;
private readonly PropertyDescriptor _property;

public bool Descending
{
get { return _descending; }
}

public PropertyDescriptor Property
{
get { return _property; }
}

public PropertyComparer(PropertyDescriptor property, bool descending)
{
if (property == null) throw new ArgumentNullException("property");
this._descending = descending;
this._property = property;
}

public int Compare(T x, T y)
{
int value = Comparer.Default.Compare(_property.GetValue(x), _property.GetValue(y));
return _descending ? -value : value;
}
}

Hi,
Nothing happens when I click on the datagridview column header. I expect it
to sort.
What am i missing please?

the datagridview's datasource is linked to the datagridviews columns to show
"ResourceID", "EnglishText" and "Translated" which are public fields of my
StringResource class. Each column sort is set to autosort.

_datasource is a bindingsource control dropped onto the form but its
contents are set up dynamically.

private System.Windows.Forms.BindingSource _dataSource;

// Clear datasource
_dataSource.Clear();
// Add records to the datasource,
ResXResourceReader reader = new
ResXResourceReader(EnglishItem.FileNamePath);
try
{
IDictionaryEnumerator enumerator = reader.GetEnumerator();
foreach (DictionaryEntry de in reader)
{
StringResource res = new StringResource((string)de.Key,
(string)de.Value,"");
_dataSource.Add(res);
}
}
finally
{
reader.Close();
}
On Thursday, March 06, 2008 8:15 AM Marc Gravell wrote:
The key thing here is that the DataGridView isn't responsible for sorting;
the underlying data-source (i.e. .DataSource of the DataSource) is.

So: what is the data-source? I have posted (to this forum) several examples
of sortable BindingList<T> implementations that I might be able to dig out?
On Thursday, March 17, 2011 12:45 AM hal21th hal21th wrote:
Dear all,



I modified AdvancedList<T> a little bit, so that it can toggle order by ascending and descending.

It's ugly but works. Hope it can help.





public class AdvancedList<T> : BindingList<T>, IBindingListView

{

PropertyComparerCollection<T> _sorts;

PropertyDescriptor _lastProperty;

ListSortDirection _lastDirection;



protected override bool IsSortedCore

{

get

{

//return base.IsSortedCore;

return _sorts != null;

}

}



protected override void RemoveSortCore()

{

//base.RemoveSortCore();

_sorts = null;

}



protected override bool SupportsSortingCore

{

get

{

//return base.SupportsSortingCore;

return true;

}

}



protected override ListSortDirection SortDirectionCore

{

get

{

//return base.SortDirectionCore;

return _sorts == null ? ListSortDirection.Ascending : _sorts.PrimaryDirection;

}

}



protected override PropertyDescriptor SortPropertyCore

{

get

{

//return base.SortPropertyCore;

return _sorts == null ? null : _sorts.PrimaryProperty;

}

}



protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)

{

if (prop.Equals(_lastProperty))

{

direction = _lastDirection == ListSortDirection.Ascending ?

ListSortDirection.Descending : ListSortDirection.Ascending;

_lastDirection = direction;

}

else

{

_lastProperty = prop;

_lastDirection = direction;

}

//base.ApplySortCore(prop, direction);

ListSortDescription[] arr = {

new ListSortDescription(prop, direction)

};

ApplySort(new ListSortDescriptionCollection(arr));

}



public void ApplySort(ListSortDescriptionCollection sortCollection)

{

bool oldRaise = RaiseListChangedEvents;

RaiseListChangedEvents = false;

try

{

PropertyComparerCollection<T> tmp

= new PropertyComparerCollection<T>(sortCollection);

List<T> items = new List<T>(this);

items.Sort(tmp);

int index = 0;

foreach (T item in items)

{

SetItem(index++, item);

}

_sorts = tmp;

}

finally

{

RaiseListChangedEvents = oldRaise;

ResetBindings();

}

}



String IBindingListView.Filter

{

get { throw new NotImplementedException(); }

set { throw new NotImplementedException(); }

}



void IBindingListView.RemoveFilter()

{

throw new NotImplementedException();

}



ListSortDescriptionCollection IBindingListView.SortDescriptions

{

get { return _sorts.Sorts; }

}



bool IBindingListView.SupportsAdvancedSorting

{

get { return true; }

}



bool IBindingListView.SupportsFiltering

{

get { return false; }

}

}



public class PropertyComparerCollection<T> : IComparer<T>

{

private readonly ListSortDescriptionCollection _sorts;

private readonly PropertyComparer<T>[] _comparers;



public ListSortDescriptionCollection Sorts

{

get { return _sorts; }

}



public PropertyDescriptor PrimaryProperty

{

get

{

return _comparers.Length == 0 ? null : _comparers[0].Property;

}

}



public ListSortDirection PrimaryDirection

{

get

{

return _comparers.Length == 0 ? ListSortDirection.Ascending

: _comparers[0].Descending ?

ListSortDirection.Descending : ListSortDirection.Ascending; ;

}

}



public PropertyComparerCollection(ListSortDescriptionCollection sorts)

{

if (sorts == null)

throw new ArgumentNullException("sorts");

_sorts = sorts;

List<PropertyComparer<T>> list = new List<PropertyComparer<T>>();

foreach (ListSortDescription item in _sorts)

{

list.Add(new PropertyComparer<T>(item.PropertyDescriptor,

item.SortDirection == ListSortDirection.Descending));

_comparers = list.ToArray();

}

}



int IComparer<T>.Compare(T x, T y)

{

int result = 0;

for (int i = 0; i < _comparers.Length; i++)

{

result = _comparers.Compare(x, y);

if (result != 0) break;

}

return result;

}

}



public class PropertyComparer<T> : IComparer<T>

{

private readonly bool _descending;

private readonly PropertyDescriptor _property;



public bool Descending

{

get { return _descending; }

}



public PropertyDescriptor Property

{

get { return _property; }

}



public PropertyComparer(PropertyDescriptor property, bool descending)

{

if (property == null) throw new ArgumentNullException("property");

this._descending = descending;

this._property = property;

}



public int Compare(T x, T y)

{

int value = Comparer.Default.Compare(_property.GetValue(x), _property.GetValue(y));

return _descending ? -value : value;

}

}

 
Top