Skip to content

DefaultView.Sort resetting problem

Jorge

Asked Sep 10, 2012, 12:24 PM EDT4 answers
Share
LinkedInX
Hi all,

I need help with this code:

 DataAdapter1.Fill(DataSet1);
 DataSet1.Tables[0].DefaultView.Sort = "sortcolumn";
 BindingSource1.DataSource =  DataSet1.Tables[0].DefaultView;

After execution, the "DataSet1.Tables[0].DefaultView.Sort" property is reset to empty string.
What might be the cause of this, and how can this be avoided?

Thanks

4 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Akkiraju Ivaturi
    0
    First check if you are getting values into dataset or not. If values are inserted into the dataset then it should work.

    Check the following code that I have created to provide you the example:
    C#
    using System.Collections.Generic;
    using System.Data;
    
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<string[]> list = new List<string[]>();
                list.Add(new string[] { "Column 1", "Column 2", "Column 3" });
                list.Add(new string[] { "Column 5", "Column 2", "Column 3" });
                list.Add(new string[] { "Column 2", "Column 2", "Column 3" });
                list.Add(new string[] { "Column 4", "Column 2", "Column 3" });
                list.Add(new string[] { "Column 6", "Column 2", "Column 3" });
                list.Add(new string[] { "Column 9", "Column 2", "Column 3" });
                DataTable table = ConvertListToDataTable(list);
                table.DefaultView.Sort = "Column1";
                DataView defaultView = table.DefaultView;
            }
            static DataTable ConvertListToDataTable(List<string[]> list)
            {
                // New table.
                DataTable table = new DataTable();
    
    
                // Get max columns.
                int columns = 0;
                foreach (var array in list)
                {
                    if (array.Length > columns)
                    {
                        columns = array.Length;
                    }
                }
    
    
                // Add columns.
                for (int i = 0; i < columns; i++)
                {
                    table.Columns.Add();
                }
    
    
                // Add rows.
                foreach (var array in list)
                {
                    table.Rows.Add(array);
                }
    
    
                return table;
            }
        }
    }
    0
  • 0
    Thanks for the quick reply,

    The data is loaded, there is a DataGridView bound to the BindingSource1 and it shows what it should exactly, except the SortedColumn property is null (as a consequence of the underlying DataView.Sort property being reset to empty string).
    Any other suggestions?
    0
  • 0
    The problem was that in the designer window, when I previously set the Sort property of the BindingSource to some value, and then later I deleted it, the designer was initializing it's value to an empty string, not to null, which was causing the problem.
    I just deleted the initialization for the Sort property from the designer code (I suppose you can't set the value to null from the designer window).
    Issue is solved.
    0
  • Akkiraju Ivaturi
    0
    Cool. Glad that your problem is resolved.
    0