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

Delete button in gridView

7 Answers 1003 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Shirya
Top achievements
Rank 1
Shirya asked on 06 Jan 2009, 06:36 PM
Hello,
I would like to have in my gridView a delete button(no commandButton) for each row added. 
When  i clicked on one of this buttons, i would like the corresponding row to be deleted.

This is what it does:
ICreate the button on cellFormatting and add an eventHandler


 Private Sub GridViewPrepack_CellFormatting(ByVal sender As System.Object, ByVal e As Telerik.WinControls.UI.CellFormattingEventArgs) Handles GridViewPrepack.CellFormatting 


        


          If Not (e.CellElement.Children.Count > 0) Then  


                    Dim myButton As New RadButtonElement 


 


                    'Display style of the Image 


                   


                    if  CType(e.CellElement.ColumnInfo,GridViewDataColumn).FieldName = "Delete" 








                            AddHandler myButton.Click, AddressOf DeleteButton_Click 


 


                            'add the button to the cell 


                            e.CellElement.Children.Add(myButton) 


 


                       


                End If 


            End If 


 


    End Sub 

Now, i would like the row to be deleted when the delete button is clicked, but i don't know how.
I tried this, but it doesn't delete the right row
 Private Sub DeleteButton_Click(ByVal sender As Object, ByVal e As EventArgs) 
        GridViewPrepack.Rows.Remove(GridViewPrepack.CurrentRow) 
    End Sub 
.
can you help me?
Thank you
==: Shirya :==

7 Answers, 1 is accepted

Sort by
0
Martin Vasilev
Telerik team
answered on 09 Jan 2009, 08:59 AM
Hello Shirya,

Thank you for contacting us.

I reviewed your code and I think that your approach is correct. The only missing part is that you have to cast CurrentRow to GridViewDataRow when using it as an argument in the Remove method:
 
Me.radGridView1.Rows.Remove(DirectCast(Me.radGridView1.CurrentRow, GridViewDataRowInfo))  

Please, update your project and if you still experience troubles send me a small example project that demonstrates the issue. It will help me to investigate your case and provide you with further assistance.

 
All the best,
Martin Vasilev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Basel Nimer
Top achievements
Rank 2
answered on 17 Apr 2010, 12:11 PM
I have the same problem and the same approach and nothing is working no matter what i try,

i tried even to use removeat instead of remove, but still it is not working


    Private Sub DeleteItem(ByVal sender As System.ObjectByVal e As System.EventArgs) 
        rgItems.GridElement.BeginUpdate() 
        'Dim x As UI.RadButtonElement = sender 
 
        rgItems.Rows.Remove(DirectCast(rgItems.Rows(1), UI.GridViewRowInfo)) 
        MsgBox(rgItems.Rows.Count) 
        'rgItems.DataSource = list 
        rgItems.GridElement.EndUpdate() 
        'rgItems.Refresh() 
        'rgItems.Rows.RemoveAt(x.Tag) 
    End Sub 

the grid has two rows, and i still getting 2 as a message each time !!!!

even the following code is not working !!!
    Private Sub DeleteItem(ByVal sender As System.ObjectByVal e As System.EventArgs) 
        ''rgItems.GridElement.BeginUpdate() 
        ' ''Dim x As UI.RadButtonElement = sender 
 
        ''rgItems.Rows.Remove(DirectCast(rgItems.Rows(1), UI.GridViewRowInfo)) 
        ''MsgBox(rgItems.Rows.Count) 
        ' ''rgItems.DataSource = list 
        ''rgItems.GridElement.EndUpdate() 
        ' ''rgItems.MasterGridViewInfo.Rows 
        ' ''rgItems.Refresh() 
        ' ''rgItems.Rows.RemoveAt(x.Tag) 
        rgItems.Rows.Clear() 
    End Sub 

what am i doing wrong?!
0
Martin Vasilev
Telerik team
answered on 21 Apr 2010, 02:45 PM
Hello Basel Nimer,

It seems that you have already received an answer in the other forum thread, where you have asked for the similar issue. Let me know if you need any additional specific information here.

Greetings,
Martin Vasilev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Basel Nimer
Top achievements
Rank 2
answered on 21 Apr 2010, 03:19 PM
You are right, i got this issue solved.

I apologize for multi posting, but i always try to seek other solutions to save your time, and mine.

thanks for the effort.

0
Imo
Top achievements
Rank 1
answered on 15 Jul 2012, 03:47 AM

Hi everyone please help me how to resolve my issue on how to delete only the selected row, i have pasted the code that i have, why is it when i try to delete the current row all rows are deleted also,
but in the code it seem only deleting the current row?

the name of my radgridview is rgvSKUDetails
and i have attached an image of the form

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        PrepareGrid()
        rgvSKUDetails.TableElement.RowHeight = 20
        rgvSKUDetails.TableElement.TableHeaderHeight = 20
        rgvSKUDetails.EnableGrouping = False
        rgvSKUDetails.Rows.Add(3, 4, 5, 6, 7)
        rgvSKUDetails.Rows.Add(3, 4, 5, 6, "comment")
        rgvSKUDetails.Rows.Add(3, 4, 5, 6, "none Comment")
    End Sub
 
Private Sub PrepareGrid()
        Dim cmbbox As New GridViewComboBoxColumn
        rgvSKUDetails.Columns.Add(cmbbox)
        cmbbox.Name = "items"
        cmbbox.HeaderText = "Items"
        cmbbox.ReadOnly = False
        cmbbox.AutoCompleteMode = AutoCompleteMode.SuggestAppend
        cmbbox.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDown
        cmbbox.AllowFiltering = True
        cmbbox.MaxWidth = 200
  
        Dim qcol As New GridViewDecimalColumn
        qcol.Name = "quantity"
        qcol.HeaderText = "Quantity"
        qcol.MaxWidth = 200
        'qcol.DecimalPlaces = 2
        qcol.AutoSizeMode = True
        rgvSKUDetails.Columns.Add(qcol)
  
        Dim priceCol As New GridViewDecimalColumn
        priceCol.Name = "price"
        priceCol.HeaderText = "Price"
        'priceCol.DecimalPlaces = 2
        priceCol.AutoSizeMode = True
        rgvSKUDetails.Columns.Add(priceCol)
  
        Dim amountCol As New GridViewDecimalColumn
        amountCol.Name = "amount"
        amountCol.HeaderText = "Amount"
        'amountCol.DecimalPlaces = 2
        amountCol.AutoSizeMode = True
        amountCol.EnableExpressionEditor = False
        amountCol.ReadOnly = True
        rgvSKUDetails.Columns.Add(amountCol)
  
        Dim comment As New GridViewTextBoxColumn
        rgvSKUDetails.Columns.Add(comment)
        comment.Name = "comment"
        comment.HeaderText = "Comment"
        comment.WrapText = True
  
  
  
        Dim cmdCol As New GridViewCommandColumn
        cmdCol.Name = "cmdDelete"
         cmdCol.MaxWidth = 45
        cmdCol.AutoSizeMode = True
        rgvSKUDetails.Columns.Add(cmdCol)
    End Sub
Private Sub rgvSKUDetails_CellFormatting(sender As System.Object, e As Telerik.WinControls.UI.CellFormattingEventArgs) Handles rgvSKUDetails.CellFormatting
      If TypeOf e.CellElement.ColumnInfo Is GridViewCommandColumn Then
          If TypeOf e.CellElement.RowInfo Is GridViewNewRowInfo Then
              'Dim radbutn As RadButtonElement = CType(e.CellElement.Children(0), RadButtonElement)
              'radbutn.Text = ""
              '' radbutn.Enabled = False
              'AddHandler radbutn.Click, AddressOf DeleteButton_Click
          Else
              Dim radbutn As RadButtonElement = CType(e.CellElement.Children(0), RadButtonElement)
              radbutn.Text = "Delete"
              AddHandler radbutn.Click, AddressOf DeleteButton_Click
          End If
      End If
  End Sub
  
  
  
  Private Sub DeleteButton_Click(ByVal sender As Object, ByVal e As EventArgs)
      If TypeOf rgvSKUDetails.CurrentRow Is Telerik.WinControls.UI.GridViewNewRowInfo Then
          Exit Sub
      End If
        
      Me.rgvSKUDetails.Rows.Remove((DirectCast(Me.rgvSKUDetails.CurrentRow, GridViewDataRowInfo)))
  End Sub
0
Nikolay
Telerik team
answered on 16 Jul 2012, 11:18 AM
Hi Imo,

The CellFormatting event is fired often. In this event, however, you are subscribing to the Click event of the button without checking if a subscription concerning the same button has already occurred. As a result, you have many subscriptions to the Click event at the same time for just one button. Further, when you press the button just once, the DeleteButton_Click method is called numerous times which results in an empty grid. I would suggest that you use the CommandCellClick event of RadGridView which is fired for the buttons of the GridViewCommandColumns, for example:

Public Class Form1
    Public Sub New()
 
        ' This call is required by the designer.
        InitializeComponent()
 
        ' Add any initialization after the InitializeComponent() call.
 
        Dim comment As New GridViewTextBoxColumn
        RadGridView1.Columns.Add(comment)
        comment.Name = "comment"
        comment.HeaderText = "Comment"
        comment.WrapText = True
 
        Dim cmdCol As New GridViewCommandColumn
        cmdCol.Name = "cmdDelete"
        cmdCol.MaxWidth = 45
        cmdCol.AutoSizeMode = True
 
        cmdCol.DefaultText = "Delete"
        cmdCol.UseDefaultText = True
 
        RadGridView1.Columns.Add(cmdCol)
 
        For i As Integer = 1 To 100
            Me.RadGridView1.Rows.Add(New Object() {i, Nothing})
        Next
 
        AddHandler RadGridView1.CommandCellClick, AddressOf RadGridView_CommandCellClick
    End Sub
 
    Private Sub RadGridView_CommandCellClick(sender As Object, e As EventArgs)
        Me.RadGridView1.Rows.Remove((DirectCast(Me.RadGridView1.CurrentRow, GridViewDataRowInfo)))
    End Sub
End Class

Note that you can use the DefaultText and the UseDefaultText properties of the column to set the text to "Delete". CommandCellClick event and the mentioned properties are covered in this article.

I hope this helps.

Kind regards,
Nikolay
the Telerik team
RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
0
Imo
Top achievements
Rank 1
answered on 16 Jul 2012, 01:43 PM
Hi Nikolay,
thank you verymuch ... and for replying to this thread(below Link) w/c for me is same purpose but diiferent approach.
http://www.telerik.com/community/forums/reply-thread.aspx?messageId=0&threadId=565435
Tags
GridView
Asked by
Shirya
Top achievements
Rank 1
Answers by
Martin Vasilev
Telerik team
Basel Nimer
Top achievements
Rank 2
Imo
Top achievements
Rank 1
Nikolay
Telerik team
Share this question
or