简体   繁体   English

C# DataGridView 列顺序

[英]C# DataGridView Column Order

In my application, I have a DataGridView which it's data source varies depening on the button you click.在我的应用程序中,我有一个 DataGridView,它的数据源因您单击的按钮而异。 EG Clicking 'Total Downloads' it will be: EG 单击“总下载量”将是:

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.每个方法通过 SQL 查询获取数据并返回该信息的数据表。

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;我读过添加:dataGridView1.AutoGenerateColumns = false; will fix this...however, this makes no difference to the order at all...将解决这个问题......但是,这对订单没有任何影响......

thanks for the help!谢谢您的帮助!

Is this a WinForms project or an Asp.net one?这是一个 WinForms 项目还是一个 Asp.net 项目?

If it's winforms you should be able to change the order the columns are displayed in by accessing your GridViews Columns DisplayIndex如果它是 winforms,您应该能够通过访问您的 GridViews Columns DisplayIndex 来更改列的显示顺序

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

My simple solution to the columns being out of order is to add this loop that sets the DisplayIndex to the Index .我对列乱序的简单解决方案是添加此循环,将DisplayIndex设置为Index

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

The Index is assigned to each column as they are added. Index在添加时分配给每列。 I'm not sure why the DisplayIndex becomes out of order, but the above script will fix it.我不确定为什么DisplayIndex会出现乱序,但上面的脚本会修复它。

This might work as well as a one-liner:这可能与单行一样有效:

grid.Columns.foreach(c => c.DisplayIndex = c.Index);

For me it did not the trick.对我来说,这不是诀窍。 One more line needed:还需要一行:

entityDataGridView.AutoGenerateColumns = false;

Regards!问候!

Try to play with the display index like this尝试像这样使用显示索引

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

I had the same problem.我有同样的问题。 I solved with:我解决了:

dataGridView.DataSource = null;
dataGridView.DataSource = MyDataTable;

I found this to be very helpful.我发现这非常有帮助。

Usage: ColumnDisplayOrder("Park Name, Author, Total Downloads", myDataGridView);用法: ColumnDisplayOrder("Park Name, Author, Total 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;
        }
    }

I had the same issue and found the reason.我遇到了同样的问题并找到了原因。 It's 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 like you'd expect it.如果你重新排列它们,顺序应该和你期望的一样。

I had the same problem with column order in datagrid, in my project data source was list of objects, through designer I added column in right order, but when I started application order was mixed.我在数据网格中的列顺序有同样的问题,在我的项目数据源是对象列表,通过设计器我按正确的顺序添加了列,但是当我开始应用程序时,顺序是混合的。 As I added binding on my properties from class I noticed that my properties are in wrong order in regard to column order in datagrid, so I added properties in class in same order like I added columns and I got correct order even if AutoGenerateColumns is set to true .当我从类中添加对我的属性的绑定时,我注意到我的属性在数据网格中的列顺序是错误的,所以我以与添加列相同的顺序在类中添加属性,即使AutoGenerateColumns设置为真的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM