DataGridView - Code Included - Want to Add Column Sorting

T

Tom

Please help me understand my sorting problem in DataGridView. From
what I've read ... clicking on the column headings should sort the
text columns. The code in question is very compact and included in
entirety below.

The combination of learning the massive DataGridView and Data Binding
has me sooo confused! As I read about DataGridView, the described
capabilities seem to be just short of miraculous? As I read about Data
Binding, I get the impression that if you "can" bind the data ... then
you should? Because done correctly, the Data Binding greatly
simplifies event handling and coding complexity.

As an introduction to DataGridView with Data Binding, I am trying to
substitute the ListView used by Petzold in his PrimevalFileExplorer to
show file details with a DataGridView.

All is well until I try to sort by columns. Setting the
DataGridView.DataSource populates the grid so easily!! The headings
created from properties is awesome. I've reduced the following code to
minimal functionality to focus on my sorting problem. It simply lists
the file details of the c: drive and allows some basic column sizing
and moving.

Even though I've used BindingList<T> ... I am not sure if the data is
"Bound" to the grid. I have tried using DataSource too, but to be
honest, I am too confused to know which direction to turn. In the past
I worked through IComparer interface requirements with ListView to get
column sorting to work. The IComparer sorting certainly challenged me.
My reading about DataGridView has led me to think the sorting
mechanisms have now been much more automated. I'm hoping this turns
out to be the case! But for now ... how to accomplish it remains a
mystery.

Thanks in advance.

-- Tom

---------------------------------------------------------

using System;
using System.IO;
using System.Windows.Forms;
using System.ComponentModel;


public class LearningDataGridView : Form
{
BindingList<Details> listOfDetails;
DataGridView grid;

[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.Run(new LearningDataGridView());
}

public LearningDataGridView()
{
Width *= 2;
Text = "LearningDataGridView";

grid = new DataGridView();
grid.Parent = this;
grid.AutoSize = true;
grid.Dock = DockStyle.Fill;
grid.AllowUserToResizeColumns = true;
grid.AllowUserToOrderColumns = true;
grid.AllowUserToAddRows = false;

listOfDetails = new BindingList<Details>();

this.Shown += new EventHandler(LearingDataGridView_Shown);
} // << End of Constructor

public void LearningDataGridView_Shown(object o, EventArgs args)
{
InitializeListOfDetails();
this.grid.DataSource = listOfDetails;
FillListOfDetails();
}

private void InitializeListOfDetails()
{
listOfDetails.AllowNew = true;
listOfDetails.AllowRemove = false;
listOfDetails.RaiseListChangedEvents = true;
listOfDetails.AllowEdit = false;
}

private void FillListOfDetails()
{
listOfDetails.Clear();
DirectoryInfo dirinfo = new DirectoryInfo("c:");

dirinfo.GetDirectories();

foreach (FileInfo file in dirinfo.GetFiles())
{
Console.WriteLine("file = {0}", file.FullName);
Details fileDetails = new Details();
fileDetails.Name = Path.GetFileNameWithoutExtension(file.Name);
fileDetails.Ext = Path.GetExtension(file.Extension);
fileDetails.Size = file.Length.ToString("N0");
fileDetails.Modified =
file.LastWriteTime.ToString("yyyy'-'MM'-'dd' 'HH':'mm");
listOfDetails.Add(fileDetails);
}
}
} // public class LearningDataGridView : Form

public class Details
{
string fileName, fileExt, fileSize, fileModified;

public string Name
{
set { fileName = value; }
get { return fileName; }
}
public string Ext
{
set { fileExt = value; }
get { return fileExt; }
}
public string Size
{
set { fileSize = value; }
get { return fileSize; }
}
public string Modified
{
set { fileModified = value; }
get { return fileModified; }
}
}
 
T

Tom

I'm the OP and reporting a solution to my sorting problem here.

What I found to work >>

http://www.personalmicrocosms.com/Pages/dotnettips.aspx?c=28&t=47#tip

The above link discusses using a DataTable and shows code for a simple
example. If I could get it to work on try #1 ... it must be easy!!

I would greatly appreciate learning other methods for populating a
DataGridView that also allow sorting. Comparing various approaches
would be a great learning tool!!

Thanks.

-- Tom
 
R

RobinS

I can tell you how to implement sorting if you have a BindingList<myObject>.
The DataGridView is covered fairly extensively in Brian Noyes's book on Data
Binding. It's a great book. It even covers sorting on more than one column
at a time, and filtering, and stuff like that, with a list<object> rather
than a data table. Let me know if you wanna know...

RobinS.
 

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