如何在没有夏季布局的情况下清除超网格的所有数据和布局

时间:2013-01-30 07:51:13

标签: c# winforms ultragrid

有没有办法在不删除摘要行的情况下设置UltraGrid的数据源?

我的Windows窗体应用程序中有一个超级网格gvResult

用户可以选择列并以其他形式对其进行排序,然后按“应用”按钮将这些更改应用于gvResult。

此外,gvResult必须显示行计数器摘要。

我在应用用户的更改之前清除了gvResult,否则排序算法不会更改为用户设置的内容。

gvResult.DataSource = new DataTable();
gvResult.DataSource = dataTable_With_New_Set_And_Sort_of_Columns;

还有另一个问题!它还会删除行计数器摘要以及gvResult的其他布局设置。我在infragistics forum上搜索,发现了以下代码;但是,第一个问题仍然存在。列排序不会改变。

BindingSource bs = new BindingSource();
bs.DataSource = typeof(DataTable);
bs.DataSource = dataTable_With_New_Set_And_Sort_of_Columns;
gvResult.DataSource = bs;

你有什么建议吗?

对不起,因为我的英语很差。

编辑:我尝试了类似下面的内容,但它再次无效:

DataTable dtTest = new DataTable();
dtTest.Rows.Clear();
dtTest = Method_That_Returns_DataTable_With_New_Set_And_Sort_of_Columns();
gvResult.DataSource = dtTest.Copy();

1 个答案:

答案 0 :(得分:0)

M_Mogharrabi,

如果我理解你想要怎么做,这可能适合你。

每当您将数据绑定到UltraGrid时,每次都会触发IntializeLayout事件,因此您需要确保将摘要行设置为在InitializeLayout函数中可见。

像这样:

    private void yourUltraGrid_InitializeLayout(object sender, InitializeLayoutEventArgs e)
    {
        // Define Global settings like you usually do
        // ....

        // Configure your UltraGrid columns.
        //// ID
        //// Caption: "ID"
        e.Layout.Bands[0].Columns[ColumnKeyA].Header.Caption = "ID";
        e.Layout.Bands[0].Columns[ColumnKeyA].Header.VisiblePosition = 0;
        e.Layout.Bands[0].Columns[ColumnKeyA].Width = 50;
        // Any additional settings you may want for this column.
        // Repeat for each column...


        // Then add this block under each column you want to add Summary value to.

        // This if function is critical to avoid summary rows from duplicating itself.
        // Check to see if the Summary does not exist.
        if (!e.Layout.Bands[0].Summaries.Exists("yourSummaryKey"))
        {
            // If it doesn't exist, create the summary.
            SummarySettings summary;
            summary = e.Layout.Bands[0].Summaries.Add("yourSummaryKey", SummaryType.Sum,
                e.Layout.Bands[0].Columns[ColumnKeyA]);

            // Change the Display Formatting if you desire.
            // This display format will change it to just numbers
            // instead of "Sum = 1234"
            summary.DisplayFormat = "{0}";

            // Change the horizontal alignment for the cell text.
            summary.Appearance.TextHAlign = Infragistics.Win.HAlign.Left;

            // Apply any other settings to this summary column
            // if needed.
            // ...
        }
    }

注意:摘要行仅适用于父带。没有办法为子带设置摘要行。

如果要重置数据网格,请在代码中添加以下内容(但不能在InitializeLayout函数中添加)

    private void btnReset_Click(object sender, EventArgs e)
    {
        yourUltraGrid.DeleteSelectedRows();

        // This will trigger the yourUltraGrid_InitializeLayout event
        // and will ensure the column settings are defined.
        yourUltraGrid.DataSource = Prototype.ugGetResourcePlanning();
    }

这将保留对排序算法所做的任何更改。因此,在此示例中:如果用户对UltraGrid进行了任何更改并更改了排序算法。点击"重置"按钮只会还原数据,而不是排序算法。

希望这有帮助。