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

4 posts, 0 answers
  1. Mike A
    Mike A avatar
    15 posts
    Member since:
    Apr 2010

    Posted 03 Apr 2015 Link to this post

    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

  2. Dess
    Admin
    Dess avatar
    1606 posts

    Posted 07 Apr 2015 Link to this post

    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.

     
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Mike A
    Mike A avatar
    15 posts
    Member since:
    Apr 2010

    Posted 11 Apr 2015 in reply to Dess Link to this post

    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

     

  5. Dess
    Admin
    Dess avatar
    1606 posts

    Posted 15 Apr 2015 Link to this post

    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.

     
Back to Top