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