Solved Delete a line from a GridView that is bound to a data table

Posted on 2012-08-18
Last Modified: 2012-08-21

I am using the following code to populate a GridView. What code would I use to
delete a line from the GridView?


    Protected Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click


            Dim i As Integer
            Dim C1, C2, C3, C4, C5, C6, C7, C8, C9 As String

            dTable.Columns.Add("Gross Amount", GetType(String))
            dTable.Columns.Add("Credit", GetType(String))
            dTable.Columns.Add("VT", GetType(String))
            dTable.Columns.Add("VAT Amount", GetType(String))
            dTable.Columns.Add("Credit VAT", GetType(String))
            dTable.Columns.Add("exVAT", GetType(String))
            dTable.Columns.Add("Category Type 1", GetType(String))
            dTable.Columns.Add("Category Type 2", GetType(String))
            dTable.Columns.Add("Category Type 3", GetType(String))

            For i = 0 To Me.GridView1.Rows.Count - 1
                C1 = Me.GridView1.Rows(i).Cells(1).Text
                If Me.GridView1.Rows(i).Cells(2).Text = "" Or Me.GridView1.Rows(i).Cells(2).Text = " " Then
                    C2 = ""
                    C2 = Me.GridView1.Rows(i).Cells(2).Text
                End If

                C3 = Me.GridView1.Rows(i).Cells(3).Text
                C4 = Me.GridView1.Rows(i).Cells(4).Text

                If Me.GridView1.Rows(i).Cells(5).Text = "" Or Me.GridView1.Rows(i).Cells(5).Text = " " Then
                    C5 = ""
                    C5 = Me.GridView1.Rows(i).Cells(5).Text
                End If
                C6 = Me.GridView1.Rows(i).Cells(6).Text
                C7 = Me.GridView1.Rows(i).Cells(7).Text
                C8 = Me.GridView1.Rows(i).Cells(8).Text
                C9 = Me.GridView1.Rows(i).Cells(9).Text

                AddLine(dTable, C1, C2, C3, C4, C5, C6, C7, C8, C9)

            If IsNumeric(Me.GrossAmount_TextBox.Text) = False Then
                'this will take care of a blank space too
                Me.GrossAmount_TextBox.Text = "0"
            End If

            If IsNumeric(txtCredit1.Text) = False Then
                'this will take care of a blank space too
                Me.txtCredit1.Text = "0"
            End If
            If IsNumeric(VATAmount_TextBox.Text) = False Then
                'this will take care of a blank space too
                Me.VATAmount_TextBox.Text = "0"
            End If
            If IsNumeric(txtCredit2.Text) = False Then
                'this will take care of a blank space too
                Me.txtCredit2.Text = "0"
            End If

            If IsNumeric(NonVAT_Amount_TextBox.Text) = False Then
                'this will take care of a blank space too
                Me.NonVAT_Amount_TextBox.Text = "0"
            End If

            AddLine(dTable, GrossAmount_TextBox.Text, txtCredit1.Text, VATType.Text, VATAmount_TextBox.Text, txtCredit2.Text, NonVAT_Amount_TextBox.Text, Cat1.Text, Cat2.Text, Cat3.Text)
            ''HERE YOU CAN CALL AddLine as many times as you want with different parameters
            GridView1.DataSource = dTable

            GridView1.Rows(0).Cells(1).Width = 98
            If Me.lblC.Visible = True Then
                GridView1.HeaderRow.Cells(2).Text = "Credit"
                GridView1.Rows(0).Cells(2).Width = 62
                GridView1.HeaderRow.Cells(2).Text = ""
                GridView1.Rows(0).Cells(2).Width = 0
                GridView1.Rows(GridView1.Rows.Count - 1).Cells(2).Text = ""
            End If

            GridView1.Rows(0).Cells(3).Width = 38
            GridView1.Rows(0).Cells(4).Width = 85

            If Me.lblC.Visible = True Then
                GridView1.HeaderRow.Cells(5).Text = "Credit VAT"
                GridView1.Rows(0).Cells(5).Width = 52
                GridView1.HeaderRow.Cells(5).Text = ""
                GridView1.Rows(0).Cells(5).Width = 0
                GridView1.Rows(GridView1.Rows.Count - 1).Cells(5).Text = ""
            End If

            GridView1.Rows(0).Cells(6).Width = 52
            GridView1.Rows(0).Cells(7).Width = 175
            GridView1.Rows(0).Cells(8).Width = 170
            GridView1.Rows(0).Cells(9).Width = 170

        Catch ex As Exception
            Me.lblAddError.Text = ex.Message
        End Try

    End Sub

    Sub AddLine(ByRef dt As DataTable, ByVal GrossAmt As String, ByVal credit As String, ByVal vatType As String, ByVal vatAmt As String, ByVal credvat As String, ByVal exVatAmt As String, ByVal cat1 As String, ByVal cat2 As String, ByVal cat3 As String)

        Dim dr As DataRow
        dr = dt.NewRow()
        dr("Gross Amount") = GrossAmt
        dr("Credit") = credit
        dr("VT") = vatType
        dr("VAT Amount") = vatAmt
        dr("Credit VAT") = credvat
        dr("exVAT") = exVatAmt
        dr("Category Type 1") = cat1
        dr("Category Type 2") = cat2
        dr("Category Type 3") = cat3

    End Sub
Question by:murbro
    LVL 26

    Accepted Solution

    Hi murbro,

    Add a button field to your gridview:
        <asp:TemplateField HeaderText="Remove Items">
                <asp:Button id="RemoveBtn"  runat="server" Text="Remove" OnClick="RemoveBtn_OnClick"/>

    Open in new window

      Protected Sub RemoveBtn_OnClick(sender As Object, e As System.EventArgs)
        Dim row As GridViewRow = sender.Parent.Parent ' get the row
        Dim rowID As Int32 = Convert.ToInt16(row.RowIndex) ' get the RowIndex
        GridView1.DeleteRow(rowID) ' If gridview supports deletion, remove the row
      End Sub

    Open in new window

    Alan ";0)

    Author Closing Comment

    Thanks very much

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now