Conditionally show or hide Command button in command column

6 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
    1805 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. 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.
  4. Steve Batten
    Steve Batten avatar
    8 posts
    Member since:
    Jan 2010

    Posted 26 Apr 2018 Link to this post

    Hi Nikolay,

    I have one question , How we can  change button text of GridViewCommandColumn 

    In all current rows I have Update/ Delete buttons but on Add new row I want Add/Cancel buttons .

    Thanks in advance. :)

  5. Dimitar
    Admin
    Dimitar avatar
    2949 posts

    Posted 26 Apr 2018 Link to this post

    Hi Steve,

    You can use the ViewCellFormatting event to change the text of the button. Here is an example:
    public RadForm1()
    {
        InitializeComponent();
        radGridView1.Columns.Add(new GridViewCommandColumn() { UseDefaultText = true, DefaultText = "Add/Remove" });
        for (int i = 0; i < 10; i++)
        {
            radGridView1.Rows.AddNew();
        }
        radGridView1.ViewCellFormatting += RadGridView1_ViewCellFormatting;
    }
     
    private void RadGridView1_ViewCellFormatting(object sender, CellFormattingEventArgs e)
    {
        if (e.Row is GridViewNewRowInfo && e.Column is GridViewCommandColumn)
        {
            var cell = e.CellElement as GridCommandCellElement;
            cell.CommandButton.Text = "Add/Cancel";
        }
    }

    I hope this will be useful. 

    Regards,
    Dimitar
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  6. Steve Batten
    Steve Batten avatar
    8 posts
    Member since:
    Jan 2010

    Posted 26 Apr 2018 in reply to Dimitar Link to this post

    Thanks it works fine for me
Back to Top