By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,671 Members | 1,556 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,671 IT Pros & Developers. It's quick & easy.

Sort Datagrid bound to custom class array

P: n/a
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
Jul 10 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
It would be better if you post with some examples of your classes.

"Ben" <yo*******@gmail.comwrote in message
news:18**********************************@m36g2000 hse.googlegroups.com...
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
Jul 10 '08 #2

P: n/a
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?
Jul 10 '08 #3

P: n/a
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<Tand 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/mic...7528c689f9ef84

Marc
Jul 10 '08 #4

P: n/a
Ben
On Jul 10, 7:28 am, "Marc Gravell" <marc.grav...@gmail.comwrote:
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<Tand 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/mic...t.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
Jul 10 '08 #5

P: n/a
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<Tsupports *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<Tto 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<Tto 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<LeadListDSlist = 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
Jul 10 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.