locked
Reset the Sort status after re-loading a DataGridView RRS feed

  • Question

  • I've got a couple of questions about using the DataGridView (mine is not bound to a source, but that might not make a difference to the answers).

     

    1) If the grid is sorted by clicking on one of the column headings and then re-loaded by another user action, the sort glyph remains on the column heading even though the data is no-longer sorted. Clicking the column heading again causes it to sort in the opposite sequence. How do I tell the grid that the data is no-longer sorted after I re-load it (and also clear the glyph)?

     

    2) I want to use FillWeight on each column along with Fill as the column sizing style to set the initial column widths after loading. I also want the user to be able to autofit any or all columns by double-clicking the separator. I want them to be able to do this even though it uses up more space than the control has so they can use the horizontal scroll bar. It seems to me that I can't have all these options at once. When I use the Fill sizing option, I don't get the horizontal scroll bar even though I specify the grid to have both bars. If I don't use the Fill option, double-clicking the separator doesn't autofit the columns. Am I asking too much or is there a combination I'm missing?

     

    Thanks in advance for any hints.

    Tuesday, March 18, 2008 10:33 PM

Answers

  • Hi,

    1). This problem is caused by the SortColumn property not being reset when you reload the rows for the DataGridView, to fix this problem, you can manually change the SortMode of the current sorted column to NotSortable to force it to clear the sort glyph,and reset it to Automatic after reloading to restore the sorting ability.

    Code Snippet

    private void button1_Click(object sender, EventArgs e)

            {

                this.dataGridView1.Rows.Clear();

                DataGridViewColumn c = this.dataGridView1.SortedColumn;

                c.SortMode = DataGridViewColumnSortMode.NotSortable;

                this.dataGridView1.Rows.Add("3", "c");

                this.dataGridView1.Rows.Add("1", "b");

                this.dataGridView1.Rows.Add("2", "a");

                c.SortMode = DataGridViewColumnSortMode.Automatic;

            }

     

     


    2). Yes, you can't have these two features at the same time. The DataGridViewAutoSizeColumnsMode.Fill setting will auto arrange the columns to filly occupy the scope of the DataGridView without showing the horizontal scrollbar. Double-clicking on the separator will autofit the columns by default if you don't set any extra settings.

    Best Regards
    Zhi-xin Ye

    Monday, March 24, 2008 3:55 AM
  • You should detemine whether the SortColumn is null or not, put the code in an IF statement as:

    if( dgvExceptions.SortedColumn != null )

    {
         DataGridViewColumn sortCol = new DataGridViewColumn();

         sortCol = dgvExceptions.SortedColumn;
         .....blalabla.....

    }

    Wednesday, March 26, 2008 9:44 AM

All replies

  • Hi,

    1). This problem is caused by the SortColumn property not being reset when you reload the rows for the DataGridView, to fix this problem, you can manually change the SortMode of the current sorted column to NotSortable to force it to clear the sort glyph,and reset it to Automatic after reloading to restore the sorting ability.

    Code Snippet

    private void button1_Click(object sender, EventArgs e)

            {

                this.dataGridView1.Rows.Clear();

                DataGridViewColumn c = this.dataGridView1.SortedColumn;

                c.SortMode = DataGridViewColumnSortMode.NotSortable;

                this.dataGridView1.Rows.Add("3", "c");

                this.dataGridView1.Rows.Add("1", "b");

                this.dataGridView1.Rows.Add("2", "a");

                c.SortMode = DataGridViewColumnSortMode.Automatic;

            }

     

     


    2). Yes, you can't have these two features at the same time. The DataGridViewAutoSizeColumnsMode.Fill setting will auto arrange the columns to filly occupy the scope of the DataGridView without showing the horizontal scrollbar. Double-clicking on the separator will autofit the columns by default if you don't set any extra settings.

    Best Regards
    Zhi-xin Ye

    Monday, March 24, 2008 3:55 AM
  • Thanks for your response. I tried what you suggested for my Sort problem (see code snippet below) and I get an error at the line where I am setting the sort mode to NotSortable. The error is "Object reference not set to an instance of an object".

     

    DataGridViewColumn sortCol = this.dgvExceptions.SortedColumn;

    dgvExceptions.UseWaitCursor = true;

    dgvExceptions.Rows.Clear();

    sortCol.SortMode = DataGridViewColumnSortMode.NotSortable;

    ListExceptions();

    sortCol.SortMode = DataGridViewColumnSortMode.Automatic;

    dgvExceptions.UseWaitCursor = false;

     

    I also tried

     

    DataGridViewColumn sortCol = new DataGridViewColumn();

    sortCol = dgvExceptions.SortedColumn;

    dgvExceptions.UseWaitCursor = true;

    dgvExceptions.Rows.Clear();

    sortCol.SortMode = DataGridViewColumnSortMode.NotSortable;

    ListExceptions();

    sortCol.SortMode = DataGridViewColumnSortMode.Automatic;

    dgvExceptions.UseWaitCursor = false;

     

    but got the same error. Unfortunately the example code that the dynamic help links to for SortMode shows how to resize a column instead.

     

    Chris

    Wednesday, March 26, 2008 9:40 AM
  • You should detemine whether the SortColumn is null or not, put the code in an IF statement as:

    if( dgvExceptions.SortedColumn != null )

    {
         DataGridViewColumn sortCol = new DataGridViewColumn();

         sortCol = dgvExceptions.SortedColumn;
         .....blalabla.....

    }

    Wednesday, March 26, 2008 9:44 AM
  • That's done it. Thanks.

     

    Wednesday, March 26, 2008 4:43 PM