Results 1 to 3 of 3

Thread: Clearing the results of a datagrid from a page

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2010
    Posts
    175

    Clearing the results of a datagrid from a page

    I have the following page and what I'd like to be able to do is that when the user clicks on the clear button that it disposes of the results of the datagridview and reloads the page. What would be the best way to go about doing that?

    Code:
    Imports System.Data.SqlClient
    Imports System.Data
    Partial Class _Default
        Inherits System.Web.UI.Page
        Protected Sub whosoncallButton_click(ByVal sender As Object, ByVal e As System.EventArgs) Handles whosoncallButton.Click
            Dim dt As New DataTable
            Dim da As New SqlDataAdapter
            Dim cmd As New SqlCommand
            Dim connectionString As String = "Initial Catalog=mdr;Data Source=xxxxx;uid=xxxxx;password=xxxxx"
            Dim con As New SqlConnection(connectionString)
            con.Open()
            cmd.Connection = con
            cmd.CommandType = CommandType.StoredProcedure
            cmd.CommandText = "getoncall"
            cmd.Parameters.AddWithValue("@subschedule", TextBox1.Text)
            Try
                da.SelectCommand = cmd
                da.Fill(dt)
                GridView1.DataSource = dt
                GridView1.DataBind()
                con.Dispose()
            Catch ex As Exception
                Response.Write("Error:" & ex.ToString)
            End Try
        End Sub
    
        Protected Sub clearButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles clearButton.Click
            TextBox1.Text = ""
            GridView1.DataSource = Nothing
        End Sub

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Clearing the results of a datagrid from a page

    You need to call DataBind method after you set the datasource to nothing
    Code:
    GridView1.DataSource = Nothing
    GridView1.DataBind()
    If you want to clear the data only but the gridview to remain, you clear the datatable that it is bound to.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jun 2010
    Posts
    175

    Re: Clearing the results of a datagrid from a page

    Stanav.

    Thank you, I posted that then found the answer.

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