Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

How do i get my datagridview to sort?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Claire - 06 Mar 2008 12:56 GMT
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();
}
Marc Gravell - 06 Mar 2008 13:15 GMT
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?
Claire - 06 Mar 2008 13:46 GMT
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
Marc Gravell - 06 Mar 2008 14:17 GMT
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/2b
7528c689f9ef84


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
Claire - 07 Mar 2008 15:51 GMT
thank you marc :)
> First - thankyou for the clear question; you posted enough to reproduce
> the issue, without posting the entire app - much appreciated.
Marc Gravell - 07 Mar 2008 15:59 GMT
You're welcome; I hope it worked!

Marc

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.