This is a migrated thread and some comments may be shown as answers.

Conditionally show or hide Command button in command column

5 Answers 549 Views
Buttons, RadioButton, CheckBox, etc
This is a migrated thread and some comments may be shown as answers.
Shamjith
Top achievements
Rank 1
Shamjith asked on 16 Dec 2008, 01:02 PM
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.

5 Answers, 1 is accepted

Sort by
0
Nikolay
Telerik team
answered on 22 Dec 2008, 03:33 PM
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.
0
Shamjith
Top achievements
Rank 1
answered on 23 Dec 2008, 04:26 AM
Thank you Nikolay. This is what i am looking for. Thanks for the help.
0
Steve Batten
Top achievements
Rank 1
answered on 26 Apr 2018, 09:44 AM

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. :)

0
Dimitar
Telerik team
answered on 26 Apr 2018, 10:55 AM
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.
0
Steve Batten
Top achievements
Rank 1
answered on 26 Apr 2018, 01:53 PM
Thanks it works fine for me
Tags
Buttons, RadioButton, CheckBox, etc
Asked by
Shamjith
Top achievements
Rank 1
Answers by
Nikolay
Telerik team
Shamjith
Top achievements
Rank 1
Steve Batten
Top achievements
Rank 1
Dimitar
Telerik team
Share this question
or