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

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

ASP.net clear a GridView bound to a data table

Hi

I am trying to clear a GridView that I loaded using the code below using the following few lines but it doesn't clear
    GridView1.DataSource = Nothing
        dTable.Clear()
        GridView1.Columns.Clear()
        GridView1.DataSource = dTable


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

        Try
            'First set back to no credits when no rows in GridView
            If Me.GridView1.Rows.Count < 1 Then
                Me.lblC.Visible = False
            End If

            If Me.CheckBox1.Checked = True Then
                'If there is even one credit then must show open columns for all
                Me.lblC.Visible = True
            End If


            If Me.VATType.Text = "" Then
                Me.lblAddError.Text = "Please select a VAT type"
                Exit Sub
            End If

            If Me.Cat1.Text = "-" Then
                Me.lblAddError.Text = "Please select Category1"
                Exit Sub
            End If
            If Me.Cat2.Text = "-" Then
                Me.lblAddError.Text = "Please select Category2 (If none Select Category1 first)"
                Exit Sub
            End If
            If Me.Cat3.Text = "-" Then
                Me.lblAddError.Text = "Please select Category3 (If none Select Category2 first)"
                Exit Sub
            End If


            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
                C2 = Me.GridView1.Rows(i).Cells(2).Text
                C3 = Me.GridView1.Rows(i).Cells(3).Text
                C4 = Me.GridView1.Rows(i).Cells(4).Text
                C5 = Me.GridView1.Rows(i).Cells(5).Text
                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)
            Next

            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.DataBind()

            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
            Else
                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
            Else
                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
0
Murray Brown
Asked:
Murray Brown
1 Solution
 
informaniacCommented:
Add

GridView1.DataBind()
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks very much
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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