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
DefaultView.Sort resetting problem
4 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Akkiraju IvaturiRank #474 · 5.2K pointsPosted Sep 10, 2012, 1:03 PM EDT
Check the following code that I have created to provide you the example:
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; } } }Jorge6 pointsPosted Sep 10, 2012, 1:15 PM EDT
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?
Jorge6 pointsPosted Sep 10, 2012, 2:03 PM EDT
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.
Akkiraju IvaturiRank #474 · 5.2K pointsPosted Sep 10, 2012, 3:17 PM EDT