C# DataGridView 列顺序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17027250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:23:34  来源:igfitidea点击:

C# DataGridView Column Order

c#

提问by user1662290

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:

在我的应用程序中,我有一个 DataGridView,它的数据源因您单击的按钮而异。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; will fix this...however, this makes no difference to the order at all...

我读过添加:dataGridView1.AutoGenerateColumns = false; 将解决这个问题......但是,这对订单没有任何影响......

thanks for the help!

谢谢您的帮助!

采纳答案by Mikey Mouse

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

回答by ihebiheb

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;
}

回答by Luke

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

对我来说,这不是诀窍。还需要一行:

entityDataGridView.AutoGenerateColumns = false;

Regards!

问候!

回答by Michael Z.

My simple solution to the columns being out of order is to add this loop that sets the DisplayIndexto the Index.

我简单的解决方案,以列是顺序出是添加这个循环,设置DisplayIndexIndex

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

The Indexis assigned to each column as they are added. I'm not sure why the DisplayIndexbecomes out of order, but the above script will fix it.

Index在添加时分配给每列。我不确定为什么会DisplayIndex出现乱序,但上面的脚本会修复它。

This might work as well as a one-liner:

这可能与单行一样有效:

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

回答by stefano

I had the same problem. I solved with:

我有同样的问题。我解决了:

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

回答by WorkSmarter

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;
        }
    }

回答by Ralf Süss

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.

如果你重新排列它们,顺序应该和你期望的一样。

回答by Anve

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 AutoGenerateColumnsis set to true.

我在数据网格中的列顺序有同样的问题,在我的项目数据源是对象列表,通过设计器我按正确的顺序添加了列,但是当我开始应用程序时,顺序是混合的。当我从类添加绑定到我的属性时,我注意到我的属性在数据网格中的列顺序是错误的,所以我以与添加列相同的顺序在类中添加属性,即使AutoGenerateColumns设置为真的