Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all the <code>DataTable</code> object is detached from <code>DataGridView</code> even if you bind it to <code>DataSource</code>, that is, if you change your <code>DataGridView</code> it will not affect your <code>DataTable</code>. And in your case you want the sorting in <code>DataGridView</code> to be reflected in <code>DataTable</code>. And so for that reason you need to catch an event every time there is changes in ordering/sorting in <code>DataGridView</code>. You need to catch therefore for that regard the <code>ColumnHeaderMouseClick</code> event of the <code>DataGridView</code>.</p> <p>Another important thing is, in achieving a synchronize sorting of both <code>DataTable</code> and <code>DataGridView</code> is the <code>DefaultView</code> method of <code>Datable</code> class that has the <code>Sort</code> property. So, what we are going to do is every time the <code>DataGridView</code> is changed sorting-wise we will sort the <code>DataTable</code> also.</p> <p>First of all we need to make a DataTable object global so that we could access it anywhere later on.</p> <pre><code>DataTable table; </code></pre> <p>Secondly, we need to initialize an event listener for <code>ColumnHeaderMouseClick</code> and for our practical purpose we will set it at <code>Form</code> constructor.</p> <pre><code>InitializeComponent(); dataGridView1.ColumnHeaderMouseClick += new DataGridViewCellMouseEventHandler(dataGridView1_ColumnHeaderMouseClick); </code></pre> <p>We then have this empty Mouse event handler:</p> <pre><code> void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) { } </code></pre> <p>And if the above method doesn't automatically come out, just copy and paste in your code.</p> <p>And for the sake of illustration we will add the <code>DataTable</code> during the <code>Form Load</code> and in this case I am going to use your own code:</p> <pre><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; </code></pre> <p>And then finally we will put some code in the <code>ColumnHeaderMouseClick</code> event:</p> <pre><code> 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. } </code></pre> <p>You could now use table object and sorted according to the sorting order of the <code>DataGridView</code>.</p> <p>Just to confirm my claim, we will make a button in your form named <code>button1</code> and when clicked it will show the first row and the sorted column value, like:</p> <pre><code> 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); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload