问题描述:

Is there any way to set the datasource of an UltraGrid without removing its summary row?

I have an ultragrid gvResult in my Windows Form application.

The user can select the columns and sort them in another form and then apply these changes to gvResult by pressing the "Apply" button.

In addition, gvResult must show a row counter summary.

I cleared gvResult before applying user's changes to it, otherwise the sorting algorithm does not change to what the user set it to.

gvResult.DataSource = new DataTable();

gvResult.DataSource = dataTable_With_New_Set_And_Sort_of_Columns;

Therein lies another problem! It removes the row counter summary too along with the other layout settings of gvResult. I searched on infragistics forum and I found the following code; however, the first problem still exists. The column sorting does not change.

BindingSource bs = new BindingSource();

bs.DataSource = typeof(DataTable);

bs.DataSource = dataTable_With_New_Set_And_Sort_of_Columns;

gvResult.DataSource = bs;

Do you have any suggestions?

Excuse me because of my poor english language.

Edit : I tried something like below but it did not work again:

DataTable dtTest = new DataTable();

dtTest.Rows.Clear();

dtTest = Method_That_Returns_DataTable_With_New_Set_And_Sort_of_Columns();

gvResult.DataSource = dtTest.Copy();

网友答案:

M_Mogharrabi,

If I understand how you want to do it, this might work for you.

Whenever you bind data to the UltraGrid, the IntializeLayout event triggers every time, so you would want to make sure you set the Summary Row to be visible in the InitializeLayout function.

Like so:

    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.
            // ...
        }
    }

Note: The Summary row only works for the parent band. There is no way to set summary row for child bands.

If you want to reset the data grid, add the following to your code (but not in the InitializeLayout function)

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

This will keep any changes made to the sorting algorithm. So in this example: If the user has made any changes to the UltraGrid and changed the sorting algorithm along with it. Clicking on the "Reset" button will only revert the data, not the sorting algorithm.

Hope this helps.

相关阅读:
Top