Sort multiple columns of Windows Datagridview

Are you searching article about Sort multiple columns of Windows Datagridview , then you can use this articel with code snippet.

This code for Sort on multiple columns of Windows Datagridview

BindingSource BS = new BindingSource();
DataTable testTable = new DataTable();
testTable.Columns.Add("Column1", typeof(int));
testTable.Columns.Add("Column2", typeof(string));
testTable.Columns.Add("Column3", typeof(string));

testTable.Rows.Add(1, "Value1", "Test1");
testTable.Rows.Add(2, "Value2", "Test2");
testTable.Rows.Add(2, "Value2", "Test1");
testTable.Rows.Add(3, "Value3", "Test3");
testTable.Rows.Add(4, "Value4", "Test4");
testTable.Rows.Add(4, "Value4", "Test3");

DataView view = testTable.DefaultView;
view.Sort = "Column2 ASC, Column3 ASC";// Sorting Column 2 and column 3
BS.DataSource = view;

DataGridViewTextBoxColumn textColumn0 = new DataGridViewTextBoxColumn();
textColumn0.DataPropertyName = "Column1";
textColumn0.SortMode = DataGridViewColumnSortMode.Programmatic;
textColumn0.HeaderCell.SortGlyphDirection = SortOrder.None;

DataGridViewTextBoxColumn textColumn1 = new DataGridViewTextBoxColumn();
textColumn1.DataPropertyName = "Column2";
textColumn1.SortMode = DataGridViewColumnSortMode.Programmatic;
textColumn1.HeaderCell.SortGlyphDirection = SortOrder.Ascending;

DataGridViewTextBoxColumn textColumn2 = new DataGridViewTextBoxColumn();
textColumn2.DataPropertyName = "Column3";
textColumn2.SortMode = DataGridViewColumnSortMode.Programmatic;
textColumn2.HeaderCell.SortGlyphDirection = SortOrder.Ascending;

dataGridView1.DataSource = BS;


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: