[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 282
  • Last Modified:

Deleting row from datagrid when the 'asterix' row appears selected

I'm trying to allow a user to delete a row from a datagrid (whose datasource is a datatable) using a 'Delete Row' button. To do this I:

Private Sub DeleteRowButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteRowButton.Click
     Dim dTotal As Decimal
     Dim deletedDataRow As DataRow1
     Dim cm As CurrencyManager = CType(Me.BindingContext(TransactionDataGrid.DataSource, TransactionDataGrid.DataMember), CurrencyManager)
      Dim transactionDataView As DataView = CType(cm.List, DataView)
      cm.RemoveAt(cm.Position)
End Sub

The problem occurs when the user is on the new row (asterix row) of the datatable and tries to delete this row. Instead of the 'asterix' row being deleted it deletes the previous row that was current. I think this occurs because the current row does not change when you click on the 'asterix' row but can appear that it way because it is highlighted. One solution I have tried is to determine if a row is the 'asterix' row. Unfortunately I have not found a way to identify this.

Any insight on this problem would be greatly appreciated. If you require some more information, please let me know.

Thanks in advance for your help.
0
andrewting
Asked:
andrewting
1 Solution
 
Brian CroweCommented:
0
 
amyhxuCommented:
When you click on the 'asterisk' row, the current row is changed to the row number of the 'asterisk' row. But when you click anywhere outside the datagrid (e.g. your delete button), the current row is changed to the last row that has data in it automatically. I tried some work around on my computer, seems it's working:

Private Sub DeleteRowButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteRowButton.Click
     Dim dTotal As Decimal
     Dim deletedDataRow As DataRow1
     Dim cm As CurrencyManager = CType(Me.BindingContext(TransactionDataGrid.DataSource, TransactionDataGrid.DataMember), CurrencyManager)
     Dim transactionDataView As DataView = CType(cm.List, DataView)
     If rowNo < CType(DataGrid1.DataSource, DataTable).Rows.Count Then
         cm.RemoveAt(cm.Position)
     End If
End Sub

Dim rowNo As Integer

Private Sub TransactionDataGrid_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TransactionDataGrid.MouseDown
    Dim hti As DataGrid.HitTestInfo = TransactionDataGrid.HitTest(TransactionDataGrid.PointToClient(Cursor.Position))
    rowNo = hti.Row
End Sub
0
 
andrewtingAuthor Commented:
Thanks amyhxu. Your solution worked perfectly.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now