• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

Locate row at datagridview after deletion.

Hi,

I have windows application with datagridview.
I am using vb2005.

In vb6 I just put code;

rstTable.Delete
rstTable.MoveNext

If rstTable.EOF AND rstTable.RecordCount > 0 then rstTable.MoveLast.

How to do it in vb2005?

Thank you.
0
emi_sastra
Asked:
emi_sastra
  • 7
  • 7
1 Solution
 
CodeCruiserCommented:
The title of the question and the details do not match in my opinion. Anyways, the datagridview is different than recordset as it displays all the data at once instead of one record at a time. To delete a row from a datagridview, you would delete it from the datatable its bound to. Like
dTable.Rows.item(i).remove
0
 
emi_sastraAuthor Commented:
Hi CodeCruiser,

The title is match to the details. I just to let you know that what I usually do in vb6.

Please see my code, after the deletion. The pointer always pointing to the last row of current page.

I want it pointing to the next row of deleted row.

Data:
a
b
c
d

The pointer before delete is at data b. Then I delete data b, the pointer go to data c, but the data c become the last row at the page.

What's wrong with my code?

Thank you.


Try
 
            Dim strCriteria As String = ERV_Global.Get_Delete_Data(dgvData)
 
            Dim strDeleteRow As String = ERV_Global.Get_Delete_Row(dgvData)
 
            Dim strKeyRow() As String = Split(strDeleteRow, ",")
            Dim strKeyValue() As String = Split(strCriteria, ",")
 
            For intCounter As Integer = 0 To strKeyRow.Length - 1
 
                Dim strKey As String = strKeyValue(intCounter)
 
                Dim intRow As Integer = strKeyRow(intCounter)
 
                Me.dgvData.Enabled = True
                Me.dgvData.CurrentCell = dgvData.Rows(intRow).Cells(1)
                Me.dgvData.Rows(intRow).Selected = True
 
                If dgvData.CurrentRow.Cells("AccNo").Value.ToString.Trim = strKey.Trim Then
                    dgvData.Rows.Remove(dgvData.CurrentRow)
                  
                    Using conApp As SqlConnection = New SqlConnection(strSQLGLMasterConnection)
                        conApp.Open()
 
                        Dim cmdSQLFinCommand As SqlCommand = conApp.CreateCommand()
 
                        Dim trxTransaction As SqlTransaction = Nothing
 
                        trxTransaction = conApp.BeginTransaction
                        cmdSQLFinCommand.Transaction = trxTransaction
 
                        strCriteria = "AccNo = '" & strKey & "'"
 
                        Dim strSqlCommand As String = "DELETE FROM " & strFileName
                        strSqlCommand &= " WHERE " & strCriteria
 
                        Try
                            cmdSQLFinCommand.CommandText = strSqlCommand
                            cmdSQLFinCommand.ExecuteNonQuery()
 
                            trxTransaction.Commit()
 
                        Catch ex As Exception
                            Throw
                        End Try
 
                    End Using
 
                    dtTMGLACCNO.AcceptChanges()
 
                End If
 
            Next
 
        Catch ex As Exception
            ERV_Global.Err_Log_Handling(ex, Me.Text)
        End Try

Open in new window

0
 
CodeCruiserCommented:
Before removing the current row, you could get the index of the next row using

NextIndex=datagridview1.currentrow.index

and then make that index selected after deleting.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
emi_sastraAuthor Commented:
Why the pointer always pointing to the last row of current page?

What will happened if the deleted row is the last row of the datatable? How to solve this problem?

Thank you.
0
 
CodeCruiserCommented:
if its the last row then subtract 1 from the index to show the new last row. The point should not be on the last row as it should be on the row you are trying to delete.
0
 
emi_sastraAuthor Commented:
It still the same happened.

What's wrong with my code?

Please revise my code if necessary.

Thank you.
Try
 
            Dim strCriteria As String = ERV_Global.Get_Delete_Data(dgvData)
 
            Dim strDeleteRow As String = ERV_Global.Get_Delete_Row(dgvData)
 
            Dim strKeyRow() As String = Split(strDeleteRow, ",")
            Dim strKeyValue() As String = Split(strCriteria, ",")
 
            For intCounter As Integer = 0 To strKeyRow.Length - 1
 
                Dim strKey As String = strKeyValue(intCounter)
 
                intRow = strKeyRow(intCounter)
 
                'Me.dgvData.Enabled = True
                'Me.dgvData.CurrentCell = dgvData.Rows(intRow).Cells(1)
                'Me.dgvData.Rows(intRow).Selected = True
 
                If dgvData.CurrentRow.Cells("AccNo").Value.ToString.Trim = strKey.Trim Then
                    dtTMGLACCNO.Rows.Item(intRow).Delete()
                  
                    Using conApp As SqlConnection = New SqlConnection(strSQLGLMasterConnection)
                        conApp.Open()
 
                        Dim cmdSQLFinCommand As SqlCommand = conApp.CreateCommand()
 
                        Dim trxTransaction As SqlTransaction = Nothing
 
                        trxTransaction = conApp.BeginTransaction
                        cmdSQLFinCommand.Transaction = trxTransaction
 
                        strCriteria = "AccNo = '" & strKey & "'"
 
                        Dim strSqlCommand As String = "DELETE FROM " & strFileName
                        strSqlCommand &= " WHERE " & strCriteria
 
                        Try
                            cmdSQLFinCommand.CommandText = strSqlCommand
                            cmdSQLFinCommand.ExecuteNonQuery()
 
                            trxTransaction.Commit()
 
                        Catch ex As Exception
                            Throw
                        End Try
 
                    End Using
 
                    dtTMGLACCNO.AcceptChanges()
 
                 
                End If
 
            Next
 
            Me.dgvData.Enabled = True
            Me.dgvData.CurrentCell = dgvData.Rows(intRow).Cells(1)
            Me.dgvData.Rows(intRow).Selected = True
 
 
        Catch ex As Exception
            ERV_Global.Err_Log_Handling(ex, Me.Text)
        End Try

Open in new window

0
 
CodeCruiserCommented:
Calling the AcceptChanges method is the most probable reason for this behaviour.
Also, the way you are deleting the row is pretty strange to me. Why are you deleting the row from dgv and not the datatable? I would have done it like this.

dtTMGLACCNO.defaultview.rowfilter="AccNo=" & accno
if dtTMGLACCNO.DefaultView.Count=1 then
   dtTNGLACCNO.DefaultView.item(0).delete
end if
0
 
emi_sastraAuthor Commented:
I have change my code, still get the same effect.

What should change?

Thank you.


  Private Function Delete_Data() As Boolean
 
        Try
 
            For Each dgvRow As DataGridViewRow In dgvData.SelectedRows
 
                strKey = dgvRow.Cells("AccNo").Value.ToString
 
                dgvData.Rows.RemoveAt(dgvRow.Index)
 
                Using conApp As SqlConnection = New SqlConnection(strSQLGLMasterConnection)
                    conApp.Open()
 
                    Dim cmdSQLFinCommand As SqlCommand = conApp.CreateCommand()
 
                    Dim trxTransaction As SqlTransaction = Nothing
 
                    trxTransaction = conApp.BeginTransaction
                    cmdSQLFinCommand.Transaction = trxTransaction
 
                    strCriteria = "AccNo = '" & strKey & "'"
 
                    Dim strSqlCommand As String = "DELETE FROM " & dtTMGLACCNO.TableName
                    strSqlCommand &= " WHERE " & strCriteria
 
                    Try
                        cmdSQLFinCommand.CommandText = strSqlCommand
                        cmdSQLFinCommand.ExecuteNonQuery()
 
                        trxTransaction.Commit()
 
                    Catch ex As Exception
                        Throw
                    End Try
 
                End Using
 
            Next
 
            dtTMGLACCNO.AcceptChanges()
 
            Fill_Control()
 
        Catch ex As Exception
            ERV_Global.Err_Log_Handling(ex, Me.Text)
        End Try
 
    End Function

Open in new window

0
 
CodeCruiserCommented:
Remove this line
dtTMGLACCNO.AcceptChanges()
0
 
emi_sastraAuthor Commented:
Yes, dtTMGLACCNO.AcceptChanges() cause that.

Why we don't need it?

Thank you.
0
 
CodeCruiserCommented:
Currently you may need it as you are deleting rows from dgv instead of the table. But if, as i suggested, you remove rows directly from the table then you wont need to call this method. What was the outcome when you removed this method?
0
 
emi_sastraAuthor Commented:
Currently you may need it as you are deleting rows from dgv instead of the table.
Ok.

But if, as i suggested, you remove rows directly from the table then you wont need to call this method.
Could my code to be made simple?

What was the outcome when you removed this method?
It works as I wanted.

Thank you.


0
 
CodeCruiserCommented:
>It works as I wanted.

We are sorted then aren't we?
0
 
emi_sastraAuthor Commented:
Nothing.

Thank you very much for your help.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

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