C # DataGridView Column Order

In my application, I have a DataGridView, which its data source depends on the button that you click. EG. By clicking "Total Downloads", it will look like this:

dataGridView1.DataSource = totalDownloads(); 

Or loading onto a player

 dataGridView1.DataSource = playerDownloads(); 

Each method receives data through an 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 colas in order: "Park Name" "Author" "Total Downloads" "Rating", However, they are included in the "Downloads", "Park Name", "Author", "Rating"

I read that add: dataGridView1.AutoGenerateColumns = false; this will fix ... however, it has nothing to do with order at all ...

thanks for the help!

+9
c #
source share
7 answers

Is this a WinForms project or one of Asp.net?

If these are winforms, you should be able to reorder the columns by accessing your columns. GridViews DisplayIndex

  dataGridView1.Columns["Park Name"].DisplayIndex = 0; // or 1, 2, 3 etc 
+16
source share

My simple solution for unordered columns is to add this loop, which sets DisplayIndex to Index .

 foreach (DataGridViewColumn col in grid.Columns) { col.DisplayIndex = col.Index; } 

Index assigned to each column as they are added. I'm not sure why DisplayIndex is failing, but the above script will fix it.

This can work just like a single line:

 grid.Columns.foreach(c => c.DisplayIndex = c.Index); 
+9
source share

Try playing with a display index like this

  private void AdjustColumnOrder() { customersDataGridView.Columns["Park Name"].DisplayIndex = 0; customersDataGridView.Columns["Author"].DisplayIndex = 1; customersDataGridView.Columns["Total Downloads"].DisplayIndex = 2; } 
+3
source share

This is not a trick for me. Another line is required:

 entityDataGridView.AutoGenerateColumns = false; 

Hello!

+3
source share

I found this to be very helpful.

Usage: ColumnDisplayOrder ("Park name, author, total number of downloads", myDataGridView);

  private void ColumnDisplayOrder(string columnList, DataGridView gridView) { var columnListArray = columnList.Split(','); for (var i = 0; i < columnListArray.Length; i++) { var gridViewColumn = gridView.Columns[columnListArray[i].Trim()]; if (gridViewColumn != null) gridViewColumn.DisplayIndex = i; } } 
+1
source share

I had the same problem. I decided with:

 dataGridView.DataSource = null; dataGridView.DataSource = MyDataTable; 
0
source share

I had the same problem and I found the reason. This is due to the order of the class properties:

  public string Downloads { get; set; } public string ParkName { get; set; } public string Author { get; set; } public float Rating { get; set; } 

gives you downloads, park name, author, rating

If you rearrange them, the order should be what you expect.

0
source share

All Articles