---
title: "Asynchronously populate gridview on button click"  
description: "Asynchronously populate gridview on button click"  
author: "Anonymous User"  
published: 2013-05-13  
updated: 2013-05-13  
canonical: https://www.mindstick.com/forum/879/asynchronously-populate-gridview-on-button-click  
category: "asp.net"  
tags: ["asp.net"]  
reading_time: 2 minutes  

---

# Asynchronously populate gridview on button click

Hi Expert! \
I would like to [populate](https://www.mindstick.com/blog/60/populate-records-in-second-listbox-according-to-the-selection-in-first-list-box) a [Gridview](https://www.mindstick.com/articles/873/update-delete-edit-gridview-in-asp-dot-net) which is inside an [update panel](https://www.mindstick.com/forum/418/is-it-possible-to-use-fileupload-control-within-the-update-panel) on [button click](https://www.mindstick.com/forum/790/form-gets-submiited-twice-on-button-click). Currently the gridview is getting populated however doesn't show up on screen. How to achive it? My line of code as following. \
public [delegate](https://www.mindstick.com/articles/777/delegate-and-event-in-c-sharp) void BindGrid_Delegate(); protected void btnSearch_Click(object sender, EventArgs e) { try { // databind of all the [controls](https://www.mindstick.com/articles/66/how-to-create-controls-at-run-time-in-csharp-dot-net) BindGrid_Delegate bd = new BindGrid_Delegate(BindGrid); IAsyncResult ar = bd.BeginInvoke(null, null); //invoking the method } catch ([Exception](https://www.mindstick.com/articles/1824/objective-c-exception-handling) ex) { ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "PageException", "alert('" + ex.Message + "');", true); } }\
[private](https://www.mindstick.com/blog/11097/java-access-modifiers-the-public-and-the-private-modifiers) void BindGrid() { try { [DataSet](https://www.mindstick.com/articles/19/dataset) ResultDataSet = GetData(); gvShowResult.DataSource = ResultDataSet; gvShowResult.DataBind(); UpdatePanel2.Update(); } catch (Exception ex) { ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "PageException", "alert('" + ex.Message + "');", true); } }\
Updatemode for [updatepanel](https://www.mindstick.com/articles/151/asp-dot-net-ajax-server-control-updateprogress-updatepanel) is conditional.\
Thanks in advance!Any help will be appreciated. \
\

## Replies

### Reply by AVADHESH PATEL

Hi Jayprakash!\
The problem could be that you call DataBind() and Update() on the non-UI thread. UI controls should only be modified on the thread that creates them.\
You can still call your GetData() method in a separate thread, e.g. using Tasks.\
In your btnSearch_Click method:\
Task.Factory .StartNew(() => GetData()) .ContinueWith(t => { gvShowResult.DataSource = t.Result; gvShowResult.DataBind(); UpdatePanel2.Update(); }, TaskScheduler.FromCurrentSynchronizationContext());\
This calls GetData() on a separate thread and executes the continuation on the UI thread.


---

Original Source: https://www.mindstick.com/forum/879/asynchronously-populate-gridview-on-button-click

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
