Home Forums WinForms controls Xceed Grid for WinForms Calling UpdateDetailGrids inside CollapseChanged event

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #14122 |

    Hi, currently I have a working unbound master/detail grid C# windows application, where I query the server and add rows to the child DetailGrids on the fly inside the appropriate CollapsedChanged event. This is for performance reasons, as it is minimises load on the server. The columns and DetailGridTemplates are all added when the grid is first initialised.

    I now have a second requirement: DetailGrid columns also need to be added on the fly based on the results returned from the server, ie: I don’t know what they are beforehand as they depend on the results of the query. This means I need to add new columns to the grid inside the same CollapsedChanged event.

    In the documentation it is clearly stated that UpdateDetailGrids should be called, however it does not seem to work inside the CollapsedChanged event. Symptoms are:
    – DetailGrid immediately collapses again
    – Any rows added are lost

    If I comment out the UpdateDetailGrids function, then the rows are visible, but not the column headings…

    Sample code from a standalone test application, which contains:
    – A Windows form called TestForm
    – An Xceed.Grid added to the form at runtime, called gridTest

    <b>private void TestForm_Load(object sender, System.EventArgs e)
    {</b>
    // initialise the grid
    gridTest.HeaderRows.Add(new Xceed.Grid.ColumnManagerRow());
    gridTest.DataRowTemplate = new Xceed.Grid.DataRow();
    gridTest.ReadOnly = true;
    gridTest.SynchronizeDetailGrids = false;

    // add grid columns
    for (int i = 0; i < 3; ++i)
    {
    gridTest.Columns.Add(new Xceed.Grid.Column(“Col_” + i.ToString(), typeof(string)));
    }

    // add detail grid template
    Xceed.Grid.DetailGrid new_detail_grid = new Xceed.Grid.DetailGrid();
    new_detail_grid.ReadOnly = true;
    gridTest.DetailGridTemplates.Add(new_detail_grid);

    // register event on detail grid collapse
    new_detail_grid.Collapse();
    <b>new_detail_grid.CollapsedChanged += new EventHandler(this.gridDetail_CollapsedChanged);</b>

    // add some rows to the grid
    for (int r = 0; r < 3; ++r)
    {
    Xceed.Grid.DataRow grid_row = gridTest.DataRows.AddNew();
    for (int c = 0; c < 3; ++c)
    {
    grid_row.Cells[c].Value = “Cell_” + r.ToString() + “_” + c.ToString();
    }
    grid_row.EndEdit();
    }
    <b>}</b>

    <b>private void gridDetail_CollapsedChanged(object sender, EventArgs e)
    {</b>
    // check if detail grid is visible
    if (((Xceed.Grid.DetailGrid)sender).Collapsed) return;

    // get references to detail grid and parent row
    <b>Xceed.Grid.DetailGrid grid_detail = (Xceed.Grid.DetailGrid)sender;</b>
    Xceed.Grid.CellRow parent_row = (Xceed.Grid.CellRow)(grid_detail).ParentDataRow;

    // get value of first cell in parent row
    string key_value = parent_row.Cells[0].Value.ToString();

    // only add rows when detail grid is first expanded
    if (grid_detail.DataRows.Count == 0)
    {
    // first add new columns to detail grid
    for (int i = 0; i < 3; ++i)
    {
    string col_name = “[” + key_value + “] ” + “Col_” + i.ToString();
    <b>grid_detail.Columns.Add(new Xceed.Grid.Column(col_name, typeof(string)));</b>

    // tell grid to update its detail grids
    <b>gridTest.UpdateDetailGrids();</b>
    }

    // add some rows to the detail grid
    for (int r = 0; r < 3; ++r)
    {
    <b>Xceed.Grid.DataRow grid_row = grid_detail.DataRows.AddNew();</b>
    for (int c = 0; c < 3; ++c)
    {
    grid_row.Cells[c].Value = “[” + key_value + “] ” + “Cell_” + r.ToString() + “_” + c.ToString();
    }
    grid_row.EndEdit();
    }
    }
    <b>}</b>

    Imported from legacy forums. Posted by IdleHand (had 2119 views)

    User (Old forums)
    Member
    Post count: 23064

    Ok I was making a mistake. But I was very close to getting it working 🙂

    Firstly, in the above example you don’t need to call UpdateDetailGrids, as the detail grid does not yet contain any rows. An important consideration. Secondly, I didn’t add a ColumnManagerRow to my DetailGridTemplate, which is why I couldn’t see any column headings when I commented out UpdateDetailGrids. duh.

    For people reading the example, it is also important to not that I have set SynchronizeDetailGrids to false, meaning I can have completely different columns in each detail grid.

    It’s always so obvious once it’s working!

    Imported from legacy forums. Posted by IdleHand (had 3079 views)

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.