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

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

1 Answer 491 Views
GridView
This is a migrated thread and some comments may be shown as answers.
George C.
Top achievements
Rank 2
Iron
Veteran
George C. asked on 06 Nov 2019, 02:14 PM

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

1 Answer, 1 is accepted

Sort by
0
Accepted
Nadya | Tech Support Engineer
Telerik team
answered on 08 Nov 2019, 11:47 AM

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.
Tags
GridView
Asked by
George C.
Top achievements
Rank 2
Iron
Veteran
Answers by
Nadya | Tech Support Engineer
Telerik team
Share this question
or