How do I programmatically add new row to gridview on button click handler?

You don't. But you can simply and straightforwardly add a new item to your grid's DataSource and you are done.

Up vote 2 down vote favorite share g+ share fb share tw.

This is my markup: " /> This is my button click handler: protected void abc_Click(object sender, EventArgs e) { } I can't find any method like gridview.Rows.Add(). C# asp.net link|improve this question asked Sep 27 '11 at 17:41Anthony3,83611670 84% accept rate.

Yes that is the easiest approach but I am trying to add controls programmatically. Gridview.Controls. Add(row); where row is instance of GridViewRow works.

But this only works for 1st time. Any idea why? – Anthony Sep 27 '11 at 17:55 Yes: when you do that, it does not go to ViewState, so it gets lost.

You'd have to keep doing it for every postback. Seriously, you are better off dealing with the DataSource. You do realize that, say, an object list can be a DataSource, right?

Maybe you can explore that. – Adrian Sep 27 '11 at 18:00 Yes I do realize that you can setup List and according to your template of your gridview it will layout items. As I said easiest and the best solution!

But I was just trying something new but I understand it is not possible. Thanks. – Anthony Sep 27 '11 at 18:03 Technically, it is possible.

It just takes a lot more work than managing a simple custom objects list. Hope it helped :) – Adrian Sep 27 '11 at 18:04.

Try adding a row to the datasource object, and then rebind the GridView.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions