user1055487
user1055487

Reputation: 261

refresh gridview after search

I have a search on a grid view which limits the results. I would like the grid view to repopulate with all entries if a. the search box is empty or b. the user hits a button to refresh.

Protected Sub btnSeach_Click(sender As Object, e As EventArgs) Handles btnSeach.Click
    StaffDetailsStaffGridView.DataSourceID = ""
    StaffDetailsStaffGridView.DataSource = ObjectDataSource1
    StaffDetailsStaffGridView.DataBind()


    If txtFnameSearch.text = " " Then
        StaffDetailsStaffGridView.DataBind()
    End If
End Sub

Protected Sub btnRefreshSearch_Click(sender As Object, e As EventArgs) Handles btnRefreshSearch.Click
    StaffDetailsStaffGridView.DataBind()
End Sub
End Class

StaffDetailsStaffGridView.DataBind() obviously doesn't work.

how do I do this properly?

Upvotes: 0

Views: 2268

Answers (4)

James Johnson
James Johnson

Reputation: 46067

If you're using a declarative datasource, you can just call DataBind() again on the GridView:

StaffDetailsStaffGridView.DataBind()

Upvotes: 0

Surinder Bhomra
Surinder Bhomra

Reputation: 2199

The best way to repopulate the GridView is by having a method specifically for binding your data and calling when needed.

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostback)
        BindGrid();
}

private void BindGrid()
{
    StaffDetailsStaffGridView.DataSource = ObjectDataSource1;
    StaffDetailsStaffGridView.DataBind();

}

protected void btnRefreshSearch_Click(object sender, EventArgs e)
{
    BindGrid(); 
}

protected void btnSeach_Click(object sender, EventArgs e)
{
    if (!String.IsNullOrEmpty(txtFnameSearch.text))
    {
        BindGrid();
    }
}

I'm assuming you are filtering your data directly via the ObjectDataSource.

Upvotes: 1

Vano Maisuradze
Vano Maisuradze

Reputation: 5909

This will not work because, after postback, gridview's datasource will be lost. So you always need to set datasource before DataBind() is called.

StaffDetailsStaffGridView.DataSource = ObjectDataSource1
StaffDetailsStaffGridView.DataBind()

You can also save ObjectDataSource1 into session and use it later for binding:

Session["MyObjectDataSource"] = ObjectDataSource1;

...

Protected Sub btnRefreshSearch_Click(sender As Object, e As EventArgs) Handles btnRefreshSearch.Click
    StaffDetailsStaffGridView.DataSource = Session["MyObjectDataSource"]
    StaffDetailsStaffGridView.DataBind()
End Sub

Upvotes: 0

Shyju
Shyju

Reputation: 218892

You should get the datasource again and set it as the DataSource and then bind again.

Something like this

Protected Sub btnRefreshSearch_Click(sender As Object, e As EventArgs) Handles btnRefreshSearch.Click

    Dim searchKey as String 
    searchKey  =txtFnameSearch.Text.Trim()
    Dim staffSearchREsults=MyService.GetSearchResults(searchKey)
    StaffDetailsStaffGridView.DataSource = staffSearchREsults
    StaffDetailsStaffGridView.DataBind()

End Sub

Assuming MyService.GetSearchResults method will return you a valid search result based on the search key.

Upvotes: 0

Related Questions