Search code examples
c#datagridviewissue-trackinggridview-sorting

C# datagridview Columns clear on sort


        var ds = new DataSet();
        ds.ReadXml(XMLFile);

        DataGridLogView.DataSource = ds.Tables["Header"];
        DataGridLogView.DataMember = "Data";
        DataGridLogView.Columns.Add("5", "Record #");
        DataGridLogView.Columns.Add("6", "Record");

The columns "5" & "6" are the problem columns that i add after my datasource loads in. I can sort any column besides the two i created(5 & 6) but when i sort any of the columns, the two columns i create will clear of all data i add and i cannot seem to figure out why it is doing this. Thanks in advance!


Solution

  •        datatable.Columns.Add("Record #", typeof(string));
           datatable.Columns.Add("Record", typeof(string));
    

    I had to add the columns to the dataset before setting the datagridviews datasource then the sorting worked perfect.