First of all the DataTable
object is detached from DataGridView
even if you bind it to DataSource
, that is, if you change your DataGridView
it will not affect your DataTable
. And in your case you want the sorting in DataGridView
to be reflected in DataTable
. And so for that reason you need to catch an event every time there is changes in ordering/sorting in DataGridView
. You need to catch therefore for that regard the ColumnHeaderMouseClick
event of the DataGridView
.
Another important thing is, in achieving a synchronize sorting of both DataTable
and DataGridView
is the DefaultView
method of Datable
class that has the Sort
property. So, what we are going to do is every time the DataGridView
is changed sorting-wise we will sort the DataTable
also.
First of all we need to make a DataTable object global so that we could access it anywhere later on.
DataTable table;
Secondly, we need to initialize an event listener for ColumnHeaderMouseClick
and for our practical purpose we will set it at Form
constructor.
InitializeComponent();
dataGridView1.ColumnHeaderMouseClick += new DataGridViewCellMouseEventHandler(dataGridView1_ColumnHeaderMouseClick);
We then have this empty Mouse event handler:
void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
}
And if the above method doesn't automatically come out, just copy and paste in your code.
And for the sake of illustration we will add the DataTable
during the Form Load
and in this case I am going to use your own code:
table = new DataTable();
table.Columns.Add("Name");
table.Columns.Add("Age", typeof(int));
table.Rows.Add("Alex", 27);
table.Rows.Add("Jack", 65);
table.Rows.Add("Bill", 22);
dataGridView1.DataSource = table;
And then finally we will put some code in the ColumnHeaderMouseClick
event:
void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (dataGridView1.SortOrder.ToString() == "Descending") // Check if sorting is Descending
{
table.DefaultView.Sort = dataGridView1.SortedColumn.Name + " DESC"; // Get Sorted Column name and sort it in Descending order
}
else
{
table.DefaultView.Sort = dataGridView1.SortedColumn.Name + " ASC"; // Otherwise sort it in Ascending order
}
table = table.DefaultView.ToTable(); // The Sorted View converted to DataTable and then assigned to table object.
}
You could now use table object and sorted according to the sorting order of the DataGridView
.
Just to confirm my claim, we will make a button in your form named button1
and when clicked it will show the first row and the sorted column value, like:
private void button1_Click(object sender, EventArgs e)
{
String sortedValue = dataGridView1.SortedColumn.Name == "Name" : table.Rows[0][0].ToString() ? table.Rows[0][1].ToString();
MessageBox.Show(sortedValue);
}