|
Forums » .NET » ASP.NET »
|
Deleting data from grid view and displaying it back by button clicks. |
Posted Date: 08 Aug 2012 Posted By:: Naren Member Level: Silver Member Rank: 1585 Points: 5
Responses:
5
|
Hi,
I need a help in my following code:
I have two buttons view and clear. I want to view gridview after inserting a row by clicking the view button. Then by clicking the clear button I want to clear both gridview and database.
My code is: protected void btnview_Click(object sender, EventArgs e) { GridView1.Visible = true;
} protected void btncleardb_Click(object sender, EventArgs e) { GridView1.Columns.Clear(); GridView1.Visible = true; }
If I click view button after inserting the data, I can see the updated gridview and it is getting cleared when I click clear button. But if I click view button again after adding data again, grid view is not displaying.
Please help me in this regard.
Thanks.
Naren Kumar.
|
Responses
|
#683541 Author: Asheej T K Member Level: Diamond Member Rank: 2 Date: 08/Aug/2012 Rating:  Points: 3 | Hi Naren Kumar,
You are not re-binding your gridview to show the newly added data. In the view button click event you need to call the method used to bind the gridview again if you wanted to display latest data from the database.
protected void btnview_Click(object sender, EventArgs e) { GridView1.Visible = true; BindGridView();//Function to bind the gridview }
Let me know if you need any more details regarding this.
Regards, Asheej T K Microsoft MVP[ASP.NET/IIS] DotNetSpider MVM
Dotnet Galaxy
| #683550 Author: Naren Member Level: Silver Member Rank: 1585 Date: 08/Aug/2012 Rating:  Points: 1 | Hi Asheej,
Thanks for your reply. I coundn't find BindGridView() function in the popup. I might missing some namespace. But I used "gridview1.databind();" . Now I can able to see updated gridtable in my page.
Thanks.
Naren Kumar.
| #683603 Author: Ravindran Member Level: Diamond Member Rank: 3 Date: 08/Aug/2012 Rating:  Points: 4 | Hi,
Because reason was your not bind data into the gridview
protected void btnview_Click(object sender, EventArgs e) { GridView1.Visible = true; GridData(); }
//here i write common method call again wherever i required void GridData() { sqlcmd = new SqlCommand("select * from emp", sqlcon); sqlcon.Open(); da = new SqlDataAdapter(sqlcmd); dt.Clear(); da.Fill(dt); GridView1.DataSource = dt; GridView1.DataBind(); sqlcon.Close(); }
Regards N.Ravindran Your Hard work never fails
| #683609 Author: Ajatshatru Upadhyay Member Level: Gold Member Rank: 19 Date: 08/Aug/2012 Rating:  Points: 3 | Hi,
What Asheej is trying to say is you have to bind the gridview in the button click event. So just call the function in which you are binding the gridview.
If you are binding the gridview in page load, just make a separate function for that and call it from page load as well as from button click event.
Hope it'll help you. Regards Ajatshatru
| #683624 Author: Naren Member Level: Silver Member Rank: 1585 Date: 08/Aug/2012 Rating:  Points: 1 | Hi All,
Thanks for your updates. Added databind function in my code. Its working!
Very thanks.
Regards, Naren.
|
|
| Post Reply |
|
|
|
You must Sign In to post a response.
|
|
|
|
 Follow us on Twitter: https://twitter.com/dotnetspider
|
|