|
-
Jun 21st, 2026, 09:23 AM
#1
Thread Starter
Hyperactive Member
Exclude last record from DatagridView while retrieving data -Using SQL Server
Hi,
How to exclude the last record from DatagridView while retrieving data (I am using a SQL Server Database)?
For example, I have 10 records for a particular customer and I want to show only 9 records (excluding the last record). The code below shows all the 10 records belongs to that customer and it needs to be modified.
Code:
Private Sub LoadSearchSalesData()
dgvSales.Columns.Clear()
Try
Dim query As String = "Select CustNo,TrNo,TrDate,Sum(TrAmount) As INVOICE_AMOUNT from SalesTable Where [CustNo] LIKE '" & txtSearchCustNo.Text & "' Group By CustNo,TrNo,TrDate ORDER BY TrNo DESC"
Dim adapter As New SqlDataAdapter(query, Connection)
Dim table As New DataTable()
adapter.Fill(table)
dgvSales.DataSource = table
Catch ex As Exception
MessageBox.Show($"Error loading Sales Data: {ex.Message}")
End Try
End Sub
Please support me with the code.
-
Jun 24th, 2026, 04:28 PM
#2
Re: Exclude last record from DatagridView while retrieving data -Using SQL Server
Get the number of rows in the datatable using the "count" method. Then delete the last record using the "Delete" method.
Code:
Dim count = datatable.rows.Count
If count > 0 Then
datatable.rows(count-1).Delete
End If
Last edited by wes4dbt; Jun 24th, 2026 at 07:34 PM.
-
Re: Exclude last record from DatagridView while retrieving data -Using SQL Server
I'm not even sure, your original query returns what you want, since you're also grouping by Transaction-Date, which implies, you have multiple Records for the same Trans-No AND Trans-Date.
Which defeats the "logic" of having a Transaction-Number/Date
What exactly are you trying to do/achieve?
EDIT:
1) Your CustNo is a String??
2) Don't use "LIKE"-operator for equality. Use "="-Symbol
3) And ffs: USE PARAMETERS --> I'll tell your user to enter "123';D rop Table SalesTable; --" (Note the single quote in there, and i had to insert a SPACE between the D and the R, because the Forum was acting up, and wouldn't accept it)
EDIT2: Correction. My Input in the EDIT above at 3) should actually cause a Syntax-error
Last edited by Zvoni; Yesterday at 08:08 AM.
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Re: Exclude last record from DatagridView while retrieving data -Using SQL Server
I've moved this to the Visual Basic .NET forum since it really isn't a database specific question.
If it turns out that this is database specific, I can always move it back. To be honest, your thread will probably get more visibility here anyways.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|