C Sharper
C Sharper

Reputation: 8626

Clearing gridview upon certain condition in vb.net

on textbox blank, i wanted to clear my gridview source

But i was not able to do it in vb.net.

After referring several answers i tried following unsuccessfull attempts:

grdUsers.rows.clear() : Does not work with vb.net

grdUsers.DataSource=""

grdUsers.columns.clear()

But it does not worked out.

Please help me to clear my datasource of gridview.

Upvotes: 2

Views: 9201

Answers (1)

Alex
Alex

Reputation: 4938

If your DataGridView is bound to a DataSource and you want to clear it then you can use the Nothing keyword followed by a DataBind().

grdUsers.DataSource = Nothing
grdUsers.DataBind()

Here is more information on the DataBind() method.


If you want to clear your rows when the text is empty in TextBox1, you would create a TextChanged event for your textbox ...

Private Sub TextBox1_TextChanged(sender As Object, e As System.EventArgs) Handles TextBox1.TextChanged
    If TextBox1.Text.Trim = "" Then 
       grdUsers.DataSource = Nothing
       grdUsers.DataBind()
    End If
End Sub

Upvotes: 6

Related Questions