add commandbutton into each radgridview rows and How to control their click event

2 posts, 1 answers
  1. George C.
    George C. avatar
    27 posts
    Member since:
    Sep 2018

    Posted 06 Nov 2019 Link to this post

    Hi,

    I want to add commandbutton to each radgridview row, and I want to know how to define each row's button click event ?

    For example, If I click on button of row 3, the button shows a string in the cell instead and if clicked back again, the button hides string and shows itself.

    I use vb.net

     

    Thanks

  2. Answer
    Nadya
    Admin
    Nadya avatar
    341 posts

    Posted 08 Nov 2019 Link to this post

    Hello George,

    I can suggest you to use GridViewCommandColumn which displays a button element on every row in RadGridView. The column responds to user input mouse clicks via the CommandCellClick event. You can handle the CellFormatting event in order to change the appearance of the button element and show the text that you want. Please refer to the following example which demonstrates this approach:

    PrivateSub SurroundingSub()
        Dim commandColumn As GridViewCommandColumn = New GridViewCommandColumn()
        commandColumn.Name = "CommandColumn"
        commandColumn.FieldName = "Title"
        commandColumn.HeaderText = "CommandColumn"
        radGridView1.MasterTemplate.Columns.Add(commandColumn)
    
     AddHandler radGridView1.CommandCellClick, AddressOf RadGridView1_CommandCellClick
     AddHandler radGridView1.CellFormatting, AddressOf  RadGridView1_CellFormatting
     AddHandler radGridView1.CellClick, AddressOf  RadGridView1_CellClick
     EndSub
    
    Private Sub RadGridView1_CellClick(ByVal sender As Object, ByVal e As GridViewCellEventArgs)
        If TypeOf e.Column Is GridViewCommandColumn Then
            If radGridView1.CurrentCell.Tag IsNot Nothing Then
                radGridView1.CurrentCell.Tag = Nothing
                Me.radGridView1.TableElement.Update(GridUINotifyAction.StateChanged)
            End If
        End If
    End Sub
    
    Private Sub RadGridView1_CellFormatting(ByVal sender As Object, ByVal e As CellFormattingEventArgs)
        If TypeOf e.CellElement Is GridCommandCellElement Then
            Dim cell As GridCommandCellElement = TryCast(e.CellElement, GridCommandCellElement)
            cell.DrawText = True
            cell.Text = "Test"
            If cell.Tag IsNot Nothing Then
                cell.CommandButton.Visibility = ElementVisibility.Collapsed
            Else
                cell.CommandButton.Visibility = ElementVisibility.Visible
            End If
        End If
    End Sub
    
    Private Sub RadGridView1_CommandCellClick(ByVal sender As Object, ByVal e As GridViewCellEventArgs)
        Dim cell As GridCommandCellElement = TryCast(sender, GridCommandCellElement)
        If cell.Tag Is Nothing Then
            cell.Tag = "Clicked"
        End If
        Me.radGridView1.TableElement.Update(GridUINotifyAction.StateChanged)
    End Sub

    I hope this helps. Should you have any other questions do not hesitate to ask.

    Regards,
    Nadya
    Progress Telerik

    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top