Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# DataGridView Column Order

Tags:

c#

In my application, I have a DataGridView which it's data source varies depening on the button you click. E.G. Clicking 'Total Downloads' it will be:

dataGridView1.DataSource = totalDownloads();

Or the downloads per player

dataGridView1.DataSource = playerDownloads();

Each method obtains data via SQL query and returns a dataTable of this information.

However, with my following code:

dataGridView1.DataSource=getStats();
public DataTable getStats()
   {
        DataTable table1 = new DataTable("Totals");
        table1.Columns.Add("Park Name");
        table1.Columns.Add("Author");
        table1.Columns.Add("Total Downloads");
        table1.Columns[2].DataType = typeof(int);
        table1.Columns.Add("Rating (Max 5)");           
        table1.Columns[3].DataType = typeof(float);
        table1.Rows.Add(name,author,download, rating);
        }
        return table1;
    }

I expected to see the colums in the order: "Park Name" "Author" "Total Downloads" "Rating" However, they come in "Downloads", "Park Name", "Author", "Rating"

I've read that adding: dataGridView1.AutoGenerateColumns = false; will fix this...however, this makes no difference to the order at all...

thanks for the help!

like image 849
user1662290 Avatar asked Jun 10 '13 15:06

user1662290


1 Answers

For me it did not the trick. One more line needed:

entityDataGridView.AutoGenerateColumns = false;

Regards!

like image 67
Luke Avatar answered Oct 07 '22 11:10

Luke