Sort Datagrid bound to custom class array

B

Ben

Does anyone have any example code showing how to sort a datagrid that
is bound to a custom class array? My code calls a web method that
returns an array of type LeadListDS. LeadListDS consists of string,
DateTime, and boolean datatypes. This returned array is then bound to
the datagrid and seems to work fine I just need to figure out this
sorting issue. Thanks

Ben
 
B

Ben

Here is the class I'm binding to the datagrid:

public class LeadListDS
{
public string LeadGUID;
public string ContactName;
public string CompanyName;
public string StateOrProvince;
public string Email;
public string LeadStatus;
public DateTime CreatedDateTime;
public string OwnerAlias;
public Boolean Unread;
public string ProductInfo;
public string CompanyAddress;

public LeadListDS()
{

}
}

------

This is the code where I call the web method that returns an array of
this class and bind to the datagrid:

dgLeads.DataSource = PP.LeadList(ContactGUID, Role, util.WSUser,
util.WSPassword);
dgLeads.DataBind();

-------
web method declaration

public LeadListDS[] LeadList(string ContactGUID, int Role, string
User, string pwd)


Anything else that would be helpful?
 
B

Ben

Do you want to sort it once, or allow sorting by clicking the column
headers?

If "once", just use Array.Sort (or putthe data in a List<T> and use
List<T>.Sort); if "column headers" then you need an IBindingList that
support sorting inside the list - something like:

http://groups.google.co.uk/group/microsoft.public.dotnet.languages.cs...

Marc

Do you have a full example of how to use the bindinglist? I'm a little
confused on how that works.

Thanks

Ben
 
M

Marc Gravell

OK; DataGridView, along with many other data-bound controls,
recognises a few well-known interfaces, such as IList (also
IListSource and ITypedList), IBindingList, IBindingListView,
ICancelAddNew and IRaiseItemChangedEvents. It uses these interfaces to
get the collection to do a lot of the work itself, such as reading /
editing rows, sorting, etc.

Fortunately, BindingList<T> supports *most* of the things that
DataGridView can use - but it doesn't support sorting. DataTable
(well, DataView) is another construct that supports these interfaces,
but that *does* support searching. Luckily we can extend
BindingList<T> to enable sorting on classes (rather than using DataRow
etc). This is exactly what the AdvancedList<T> [cited above] does.

Note, however, that all data-binding [what you are doing] is by
default based around properties, not fields; so here follows an
example of your class using properties with AdvancedList<T> to make a
bindable, sortable, editable table. Note that I've used C# 3 (VS2008)
syntax for brevity, but the same can be done in C# 2 (VS2005) - it
just takes a few more lines, and it is late here... Note that I haven'
re-posted AdvancedList<T>; see my previous link for that.

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
static class Program
{
static void Main()
{
AdvancedList<LeadListDS> list = new
AdvancedList<LeadListDS>();
list.Add(new LeadListDS { CompanyName = "Foo", ContactName =
"Fred", StateOrProvince = "Arizona", LeadGUID = "0x000" });
list.Add(new LeadListDS { CompanyName = "Bar", ContactName =
"Jojo", StateOrProvince = "Boston", LeadGUID = "0x001" });
Application.EnableVisualStyles();
Application.Run(new Form {
Controls = {
new DataGridView {
Dock = DockStyle.Fill,
DataSource = list
}
}
});
}
}

public class LeadListDS
{
[Browsable(false)]
public string LeadGUID { get; set; }
[DisplayName("Contact Name")]
public string ContactName { get; set; }
[DisplayName("Company Name")]
public string CompanyName { get; set; }
[DisplayName("State / Province")]
public string StateOrProvince { get; set; }
// ... [snip more props]
public LeadListDS() {}
}


Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top