更改 UltraWinGrid 的列顺序

发布于 2025-01-05 11:46:12 字数 263 浏览 7 评论 0原文

我的表单中有一个 UltraWinGrid,用户允许更改它的列的顺序。我的表单中有一个按钮,它应该将该网格的数据源作为数据表发送到生成报告的类。 我使用以下代码将 DataSource 转换为 DataTable:

(DataTable)UltraGrid1.DataSource

但问题是这个新 DataTable 中的列顺序不是用户设置的可见顺序... 我怎样才能将UltraWinGrid的数据源更改为我现在在屏幕上的网格中看到的数据源?

I have a UltraWinGrid in my form that the user allowed to change the sequence of columns of it.also i have a button in my form that it should send the DataSource of this Grid as a DataTable to a class that generates report.
I have used following code to cast the DataSource to DataTable:

(DataTable)UltraGrid1.DataSource

but the problem is that the sequence of columns in this new DataTable is not the visible sequence that the user sets...
How can i change the DataSource of UltraWinGrid to what i can see in Grid on screen now??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

桃扇骨 2025-01-12 11:46:12

WinGrid 数据源包含显示网格时分配给属性的相同对象。
如果用户通过 WinGrid 界面更改列的顺序或可见性,则基础数据源根本不受影响。< br> 我想到的唯一解决方案(对于大型表来说非常昂贵)是 DataTable Copy() 方法来获取要处理的不同表,然后循环遍历grid.DisplayLayout.Band[0].Columns 并对网格上隐藏的列 (Column.Hidden) 使用复制的 DataTable Remove() 方法。
棘手的部分是列的顺序。
DataColumn 提供了 SetOrdinal 方法来更改列顺序,但是,我想您需要从索引零开始向上调用此方法。因此,您需要使用 Column.Header.VisiblePosition 属性在网格列上进行另一个循环。
但是现在还有其他问题:
首先 - 必须删除 DataTable PrimaryKey,因为如果隐藏该 PrimaryKey,则会阻止使用 Remove() 方法
第二 - VisiblePosition 索引不能是连续的,如果在 SetOrdinal 中使用,可能会指向超出范围的项目。

因此,让我们总结一下此示例代码中的所有内容:(需要 Collection.Generics 和 Linq)

Dictionary<int, string> gPos = new Dictionary<int,string>();
DataTable dtCopy = (grid.DataSource as DataTable).Copy();
dtCopy.PrimaryKey = null;
foreach(UltraGridColumn gCol in grid.DisplayLayout.Bands[0].Columns)
{
    if(gCol.Hidden == true)
        dtCopy.Columns.Remove(gCol.Key);
    else
      gPos.Add(gCol.Header.VisiblePosition, gCol.Key);
}
var list = gPos.Keys.ToList();
list.Sort();
int realPos = 0;
foreach (var key in list)
{
    dtCopy.Columns[gPos[key]].SetOrdinal(realPos++);
}

The WinGrid Datasource contains the same object assigned to the property when you display the grid.
If your user changes order or visibility of the columns via WinGrid interface the underlying Datasource is not affected at all.
The only solutions (very expensive for large tables) that comes to my mind is the DataTable Copy() method to obtain a different table to work on, then, loop through the grid.DisplayLayout.Band[0].Columns and use the copied DataTable Remove() method for columns that are hidden on the grid (Column.Hidden).
The tricky part is the order of columns.
DataColumn provides the SetOrdinal method to change the columns order, but, I suppose you need to call this method starting from index zero upward. So you need another loop on the grid columns using Column.Header.VisiblePosition property.
But there are other problems now:
First - The DataTable PrimaryKey must be removed because, if hidden, prevents the Remove() method
Second - The VisiblePosition index could not be consecutive and, if used in SetOrdinal could point to an out of range item.

So let summarize everything in this example code: (requires Collection.Generics and Linq)

Dictionary<int, string> gPos = new Dictionary<int,string>();
DataTable dtCopy = (grid.DataSource as DataTable).Copy();
dtCopy.PrimaryKey = null;
foreach(UltraGridColumn gCol in grid.DisplayLayout.Bands[0].Columns)
{
    if(gCol.Hidden == true)
        dtCopy.Columns.Remove(gCol.Key);
    else
      gPos.Add(gCol.Header.VisiblePosition, gCol.Key);
}
var list = gPos.Keys.ToList();
list.Sort();
int realPos = 0;
foreach (var key in list)
{
    dtCopy.Columns[gPos[key]].SetOrdinal(realPos++);
}
妞丶爷亲个 2025-01-12 11:46:12

您可以循环遍历列,然后使用 Header.VisiblePosition 属性来确定列的顺序。确定顺序后,您需要在数据表中设置适当的顺序。

You could loop through the columns and then use the Header.VisiblePosition property to determine the order of the columns. Once you determine the order you would need to set the appropriate order in your DataTable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文