From: Ivan on
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/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

Previous Posts In This Thread:

On Thursday, March 06, 2008 7:56 AM
Claire wrote:

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 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
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 06, 2008 8:46 AM
Claire wrote:

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 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

On Thursday, March 06, 2008 9:17 AM
Marc Gravell wrote:

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

On Friday, March 07, 2008 10:51 AM
Claire wrote:

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

On Friday, March 07, 2008 10:59 AM
Marc Gravell wrote:

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/tutorials/aspnet/bf15c41b-6510-403e-9af8-f5fd987fafb1/crypto-obfuscator-for-ne.aspx