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

How to add a column to RadGridView with a button in some cells

3 Answers 1106 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Mike A
Top achievements
Rank 1
Mike A asked on 03 Apr 2015, 07:10 PM

How can I create a button inside a column for some row and not the other.

 

I tried to use the command line but this will add a button to every row

 

here is what I have done

 

            GridViewCommandColumn commandColumn2 = new GridViewCommandColumn();
            commandColumn2.Name = "UpdateInfo";
            commandColumn2.UseDefaultText = true;
            commandColumn2.DefaultText = "View More Info";
            commandColumn2.FieldName = "UpdateInfo";
            commandColumn2.HeaderText = "";
            radGridView1.MasterTemplate.Columns.Add(commandColumn2);
            radGridView1.CommandCellClick += new CommandCellClickEventHandler(radGridView1_CommandCellClick);

 

How can I only show the button for some rows and not the others

3 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 07 Apr 2015, 09:51 AM
Hello Mike,

Thank you for writing.

You can use the CellFormatting event and hide the GridCommandCellElement.CommandButton for the desired rows:
private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
    GridCommandCellElement commandCell = e.CellElement as GridCommandCellElement;
    if (commandCell != null)
    {
        if (e.RowIndex % 2 == 0)
        {
            commandCell.CommandButton.Visibility = Telerik.WinControls.ElementVisibility.Collapsed;
        }
        else
        {
            commandCell.CommandButton.Visibility = Telerik.WinControls.ElementVisibility.Visible;
        }
    }
}

I hope this information helps. Should you have further questions, I would be glad to help.
 
Regards,
Dess
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
0
Mike A
Top achievements
Rank 1
answered on 11 Apr 2015, 07:41 PM

Hello,

Thank you for your help. I have been trying to get your code to work in my application with not much of luck.

Here is what I have, a textBox and a button. The textBox is there to capture a unique value from the database. based of the results that are returned from the database the system will show the button or not.

For example a user type in "12345" and then clicks "Add". The system will then do a database look up and add the row to the gridview but depending of the "_itemType" value that is returned from the database I make a decision to show the button or not. or even to rename the button if needed.

 

my code will need to be something like this

 

GridViewDataRowInfo rowInfo = new GridViewDataRowInfo(radGridView1.MasterView);
                if (i._itemType == "act") {
                    //rowInfo.Cells["UpdateInfo"].ColumnInfo.IsVisible = true;
                } else if (i._itemType == "bct"){

                   //rowInfo.Cells["UpdateInfo"].ColumnInfo.IsVisible = true;

                   //change the button label
                } else {
                    //rowInfo.Cells["UpdateInfo"].ColumnInfo.IsVisible = false;
                }

 

How can I get this line of code to work in on button click event

 

GridCommandCellElement commandCell = e.CellElement as GridCommandCellElement;

 thank you for your help

 

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 15 Apr 2015, 03:12 PM
Hello Mike,

Thank you for writing back.

If I understand your requirement correctly, you will add a row to the RadGridView after a query to database is performed. Afterwards, considering a specific information (item type) you will display/hide the GridCommandCellElement.CommandButton. One possible approach is to use the Tag property of the GridViewRowInfo and store any useful information. Thus, in the CellFormatting event you can access this information and determine whether to show the button or not. The provided code snippet will hide the entire column in RadGridView which will affect all rows. It will not hide the CommandButton per row. The CellFormatting event is the appropriate place to manipulate the button's Visibility

Alternatively, you can use the grid in bound mode. Thus, you can store the necessary information in the DataBoundItem.

If it is not the exact requirement, please give us some more details about the exact requirement that you are trying to achieve. Thank you in advance.

I hope this information helps. If you have any additional questions, please let me know.

Regards,
Dess
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
Tags
GridView
Asked by
Mike A
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Mike A
Top achievements
Rank 1
Share this question
or