Conditionally show or hide Command button in command column

3 posts, 0 answers
  1. Shamjith
    Shamjith avatar
    15 posts
    Member since:
    Sep 2008

    Posted 16 Dec 2008 Link to this post

    Hi,
            I am using Radgridview for winforms. I have a grid view with three columns. First column is a text box column and the second and third are button columns. Name of the command buttons are Apply and Remove respectively. When the form is loaded for the first time, Apply button in each row should be visible and Remove button must be in hidden state . When clicked on the Apply button it should become invisible and the remove button curresponding to that row should become visible. I have written the login in RowFormatting event and uses a boolean variable to make sure that this particular functionality happens only for the first time, that is when the form is loaded.


     

    private void rgvRulesList_RowFormatting(object sender, RowFormattingEventArgs e)

     

    {
    if (!_assignedRules && slNo == ruleCount - 1)

     

    {

    ShowOrHideGridButtons();

    }

     

    }


     

    private

    void ShowOrHideGridButtons()

     

    {

     

    Guid ruleId = Guid.Empty;

     

     

    foreach (GridViewDataRowInfo dataRowInfo in rgvRulesList.Rows)

     

    {

     

    GridCommandCellElement gridViewCellApply = (GridCommandCellElement)dataRowInfo.Cells["Apply"].CellElement;

     

     

    GridCommandCellElement gridViewCellRemove = (GridCommandCellElement)dataRowInfo.Cells["Remove"].CellElement;

     

     

    if (gridViewCellApply == null || gridViewCellRemove == null)

     

    {

     

    continue;

     

    }

     

    RadButtonElement rbApply = (RadButtonElement)gridViewCellApply.Children[0];

     

     

    RadButtonElement rbRemove = (RadButtonElement)gridViewCellRemove.Children[0];

     

     

    ruleId =

    new Guid(gridViewCellApply.Value.ToString());

     

     

    foreach (EntityRulesMapping mapping in _entityRuleMappingList)

     

    {

     

    if (mapping.RuleId == ruleId)

     

    {

    rbApply.Visibility =

    ElementVisibility.Hidden;

     

    rbRemove.Visibility =

    ElementVisibility.Visible;

     

     

    break;

     

    }

     

    else

     

     

     

     

    {

    rbApply.Visibility =

    ElementVisibility.Visible;

     

    rbRemove.Visibility =

    ElementVisibility.Hidden;

     

    }

    }

    // end of inner for each

     

     

     

     

    }

    // end of outer for each

     

     

     

     

    _assignedRules =

    true;

     

    }

     

    But i could not arrive at the required solution. But when i click any of the button, this rowformatting works and the required result is achieved. I think this is because, the rowformatting event happens when some user interaction happens with the grid. Please help me on this.

  2. Nikolay
    Admin
    Nikolay avatar
    1803 posts

    Posted 22 Dec 2008 Link to this post

    Hi Shamjith,

    Thank you for the question.

    In order to get the desired result, you should use the CellFormatting event handler instead of the RowFormatting one. In addition, please note that when you want to change something regarding the CellElements and RowElements, you should get them from the event args of the event handler. Otherwise, if you try accessing them, in a foreach loop, traversing the Rows collection of RadGridView, you may end up with an exception, since a GridViewDataRowInfo may not have a corresponding CellElement at a specific moment.

    Concerning your scenario, it will be best if you keep a value in a separate column that governs which will be the visible button - "Apply" or "Remove" - for a specific row. I have demonstrated the approach in the sample project attached.

    I hope this helps. If you have additional questions, feel free to contact me.

    Best wishes,
    Nikolay
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Shamjith
    Shamjith avatar
    15 posts
    Member since:
    Sep 2008

    Posted 22 Dec 2008 Link to this post

    Thank you Nikolay. This is what i am looking for. Thanks for the help.
Back to Top