gpt4 book ai didi

c# - 在 C# 中重新排列列

转载 作者:行者123 更新时间:2023-11-30 22:45:45 26 4
gpt4 key购买 nike

如何从 C# 中的 datagridview 中获取重新排列的列?我需要按顺序获取当前显示的所有列。我可以通过设置 datagridview.AutoGenerateColumns=false 获得默认的列顺序,但即使在我重新排列表中的列之后,我得到的仍然是相同的旧顺序。

编辑:基本上我需要的是能够根据显示索引遍历 datagridview 的所有列

谢谢

最佳答案

使用 DisplayIndex property您的 DataGridView 列。

DisplayIndex 属性:

Gets or sets the display order of the column relative to the currently displayed columns.

编辑:
它很丑陋,我敢肯定人们会指指点点并大笑,但这可能会让您对需要改进的实现有一个粗略的了解。

创建一个包含三列的 DGV 并像这样设置它们的 DisplayName 属性:

    dataGridView1.Columns[0].DisplayIndex = 1;
dataGridView1.Columns[1].DisplayIndex = 2;
dataGridView1.Columns[2].DisplayIndex = 0;

添加以下代码以遍历所有列以按 DisplayIndex 顺序访问 DGV 中的列:

    for (int index = 0; index < dataGridView1.Columns.Count; index++) {
foreach (DataGridViewColumn c in dataGridView1.Columns) {
if (c.DisplayIndex == index) {
// You've got your next column.
Console.WriteLine(c.Name);
}
}
}

关于c# - 在 C# 中重新排列列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2915170/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com