none
DataGridView initial selection

    Question

  • Hi All,
    I have a TabControl with 2 pages.
    The first has a tree and the second has a DataGridView.
    Both show the same data and so I want both to show the same selection.

    I've got an AfterSelect event on the tree that sets the grid's selected row to match the tree.
    This works, but the first time you make the grid visible it selects the first row.

    Any way I can make it keep the selection I put in programatically instead of just selecting the first row ?

    C# in .NET2.0 if it matters.

    Thanks.
    Tuesday, November 17, 2009 12:58 AM

Answers

  • Thanks for the reply Tamer Oz.
    Your tip combined with some application changes has solved the problem.
    It wasn't quite as simple as just adding a VisibleChanged handler since the Selection handlers are non trivial.

    Here's how I found the problem and sorted it out:

    I've got breaks in the grids VisibleChanged and SelectionChanged handlers.

    I select an item on my tree (and code behind it select the same object in the grid)
    - Grid's SelectionChanged is triggered and appears to be selecting the correct row of the (via row.Selected = true)

    I click the tab to view the grid
    - I get another SelectionChanged (this is .NET selecting row 0) (backing code selects item 0 in the tree)
    - Then comes the VisibleChanged (but the tree already has the wrong value, so if I set the grid's selection from the tree...)

    Solution is:
    In the grids SelectionChanged I don't modify the tree's selection unless the grid has been visible
    In the grids VisibleChanged I set the flag to indicate the grid has been visible (so now grid selections update the tree)

    It doesn't feel quite right to me, but it works and I can't see any cases it will cause problems.

    Thanks again.
    • Marked as answer by __JB__ Tuesday, November 17, 2009 11:52 PM
    Tuesday, November 17, 2009 11:49 PM

All replies

  • Hi,

    You can set the rows or cells as selected in visible changed event of datagridview.
    Tuesday, November 17, 2009 4:24 AM
  • Thanks for the reply Tamer Oz.
    Your tip combined with some application changes has solved the problem.
    It wasn't quite as simple as just adding a VisibleChanged handler since the Selection handlers are non trivial.

    Here's how I found the problem and sorted it out:

    I've got breaks in the grids VisibleChanged and SelectionChanged handlers.

    I select an item on my tree (and code behind it select the same object in the grid)
    - Grid's SelectionChanged is triggered and appears to be selecting the correct row of the (via row.Selected = true)

    I click the tab to view the grid
    - I get another SelectionChanged (this is .NET selecting row 0) (backing code selects item 0 in the tree)
    - Then comes the VisibleChanged (but the tree already has the wrong value, so if I set the grid's selection from the tree...)

    Solution is:
    In the grids SelectionChanged I don't modify the tree's selection unless the grid has been visible
    In the grids VisibleChanged I set the flag to indicate the grid has been visible (so now grid selections update the tree)

    It doesn't feel quite right to me, but it works and I can't see any cases it will cause problems.

    Thanks again.
    • Marked as answer by __JB__ Tuesday, November 17, 2009 11:52 PM
    Tuesday, November 17, 2009 11:49 PM