Results 1 to 4 of 4

Thread: Exclude last record from DatagridView while retrieving data -Using SQL Server

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    370

    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.

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,514

    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.

  3. #3
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,264

    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

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,395

    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.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width