none
Sorting a DataGridView Programatically

    Question

  • Hi there,

    I have a single column datagridview which has been loaded via the "Add" option rather than databinding.

    When I click on the column header I get a glyph sorting down. Thats fine but it is already sorted down.  So to reverse the sort order I have to click the heading twice.

    I have tried to use the dataGridView.Sort function but I keep getting an error.

    I need to click on the heading and immediately sort DESC rather than ASC first. How do I do that programatically save reloading the whole DGV?

    My DGV and single column is set as so:

    dgvAssets.Columns.Add("assets", "Assets")

    I have tried to use the sort function as so:

    dgvAssets.sort(assets)

    But this is wrong and I cannot work out how the syntax works.

    Hope you can help.

    Kind Regards
    Simon.
    Sunday, January 27, 2008 12:47 AM

Answers

  • Hi, ss2030,

     

    Based on my understanding,  you want to sort your DataGridViewColumn DESC at the first time when you click the column header, don't you?

     

    Well, you should change the SortMode of this column to Programmatic and sort the column when the mouse is clicking the header cell.

    For example,

     

    Code Snippet

            private void Form1_Load(object sender, EventArgs e)

            {

                dataGridView1.Columns.Add("assets", "Assets");

                dataGridView1.Columns["assets"].SortMode = DataGridViewColumnSortMode.Programmatic;

                //Change the sort mode to programmatic

                dataGridView1.Rows.Add("hello");

                dataGridView1.Rows.Add("world");

     

                dataGridView1.ColumnHeaderMouseClick += new DataGridViewCellMouseEventHandler(dataGridView1_ColumnHeaderMouseClick);

                //Sort the column when mouse is clicking the header

            }

     

            bool desc = false;

            void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)

            {

                if (!desc)

                {

                    dataGridView1.Sort(dataGridView1.Columns["assets"], ListSortDirection.Descending);

                    dataGridView1.Columns["assets"].HeaderCell.SortGlyphDirection = SortOrder.Descending;

                    //sort desc

                }

                else

                {

                    dataGridView1.Sort(dataGridView1.Columns["assets"], ListSortDirection.Ascending);

                    dataGridView1.Columns["assets"].HeaderCell.SortGlyphDirection = SortOrder.Ascending;

                    //sort asc

                }

                desc = !desc;

            }

     

     

    More info

    http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumnsortmode.aspx

     

    Hope this helps,

    Regards

    Wednesday, January 30, 2008 6:37 AM

All replies

  • ...anybody??
    Monday, January 28, 2008 10:21 AM
  • Hi, ss2030,

     

    Based on my understanding,  you want to sort your DataGridViewColumn DESC at the first time when you click the column header, don't you?

     

    Well, you should change the SortMode of this column to Programmatic and sort the column when the mouse is clicking the header cell.

    For example,

     

    Code Snippet

            private void Form1_Load(object sender, EventArgs e)

            {

                dataGridView1.Columns.Add("assets", "Assets");

                dataGridView1.Columns["assets"].SortMode = DataGridViewColumnSortMode.Programmatic;

                //Change the sort mode to programmatic

                dataGridView1.Rows.Add("hello");

                dataGridView1.Rows.Add("world");

     

                dataGridView1.ColumnHeaderMouseClick += new DataGridViewCellMouseEventHandler(dataGridView1_ColumnHeaderMouseClick);

                //Sort the column when mouse is clicking the header

            }

     

            bool desc = false;

            void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)

            {

                if (!desc)

                {

                    dataGridView1.Sort(dataGridView1.Columns["assets"], ListSortDirection.Descending);

                    dataGridView1.Columns["assets"].HeaderCell.SortGlyphDirection = SortOrder.Descending;

                    //sort desc

                }

                else

                {

                    dataGridView1.Sort(dataGridView1.Columns["assets"], ListSortDirection.Ascending);

                    dataGridView1.Columns["assets"].HeaderCell.SortGlyphDirection = SortOrder.Ascending;

                    //sort asc

                }

                desc = !desc;

            }

     

     

    More info

    http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumnsortmode.aspx

     

    Hope this helps,

    Regards

    Wednesday, January 30, 2008 6:37 AM
  • Fantastic.  converted it to vb and it works!  thanks Yu Guo.
    Wednesday, January 30, 2008 12:27 PM