2

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.

C Sharper
  • 8,284
  • 26
  • 88
  • 151

1 Answers1

6

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
Community
  • 1
  • 1
Alex
  • 4,821
  • 16
  • 65
  • 106