Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Apr 14, 2012 12:25 PM by smilerose
Mar 22, 2012 05:32 AM|LINK
I am binding Gridview with collection at page_Load event. Note that i am using condition if(!IsPostBack). My code look like
GridView.DataSource = Manager.GetCollection(); //Where GetCollection return ObservableCollection
when I debug my code at some event lets say Button_click event, and i found that GridView.DataStore is null. Can any one please inform me what is root cause.
2nd. I am using Bind() method to bind Gridview template for Two-way-binding. can you share sample code, where two-way-binding take place.
Mar 22, 2012 09:38 AM|LINK
Please post the code. At least HTML of the gridview and the code you Databind it. Have you specified the correct Id of the gridView.?
Mar 22, 2012 01:10 PM|LINK
GridView uses the DataSource only to DataBind. On PostBack the page is built from scratch, and it won't remember what you did the first time. If you set DataSource the first time, it won't remember that on PostBack. So you will have to set it again. But
only if you need to DataBind. (If you do it always, you will likely get data even when you don't need it.)
Two-way binding is most useful when using a DataSource Control in .aspx, rather than DataBinding in code-behind. It will tell GridView what data to pass to the DataSource when doing an Update.
If you are working in 4.0, you can also use it for DataBinding in code-behind. In the RowUpdating handler, you will find the values of all things with Bind on them in e.NewValues. (And if you use BoundField and the likes, you will find the values of those
fields there too.) This will eliminate the need to extract the data from the Row yourself.
Mar 26, 2012 06:46 AM|LINK
Thanks for your response. I know what PostBack do in asp.net pages. As I m WPF develepor therefore i dont know two-way binding in Asp.net. In WPF i bind any dataContext to source, and set UpdateSourceTrigger Property to Two-Way binding. Also note that
Binding context is any collection which implements INotifyPropertyChangedinterafce. So when i update data in any field, it notify collection, it means that binding context is directly updating.
Using same idea, I want to implement such binding in asp.net, so that i minimize my code. I dont want to write any code in row_upadate or any other events. Just bind datasource at start, Bind each Gridview itemtemplate using bind() method (as bind() support
two-way binding), and get updated collection (Datasource) at end.
So what you say, how i will do this. How GridView Datasource should be avilabe to entire page (as static do this).
Mar 26, 2012 07:24 AM|LINK
You can try the ObjectDataSource control to bind GridView. Or handle the RowUdpating event of GridView for doing update manually.
Mar 27, 2012 09:45 AM|LINK
WPF is much more like Forms. It is much more connected. ASP.NET is seriously disconnected. Because of that, automatic update of data is not going to work. However, once you get the hang of it, you can come quite close.
To minimize code, you should have a look at DataSource Controls. These are Controls in .aspx that don't render, but provide a link between data and the page. GridView can be linked to a DataSource Control by setting the DataSourceID property to the ID of
the DataSource Control. If you don't bind GridView that way, you will need code-behind to do all operations. GridView won't do data operations on any collection. It can get values from a DataSource, and send values to it. What the values mean is the business
of either the DataSource Control, or the code-behind that handles the events.
Although GridView can get values from any collection through the IEnumerable interface automatically, all other operations will need more.
The most interesting DataSource Controls are
In most cases you will need to write something for each operation. I am guessing EntityDataSource is closest to what you are used to in terms of automation.
If you have a simple collection, ObjectDataSource is probably the least amount of coding, as you will only need to make the code to do the actual operations, with values you get in parameters. (GridView will use Bind to get the values of the parameters.)
Unless you find a way to use EntityFramework so you can use the EntityDataSource Control, I don't think you can do this code-less.
To get a feel for it, you could see the examples on
the msdn page for SqlDataSource and
the msdn page for ObjectDataSource. (Sadly, the one for EntityDataSource doesn't have an example.)
Apr 14, 2012 12:25 PM|LINK
I understand you discussion, and thanks lot for providing basic idea. Actually I am develeping my project as entity framework, but customized. Simply say I bind each entiy with collection, and persist as entityframe work do.
After ur response my first approach will be "convert Collection data into datasource and get updated data from datasource." I think this approach will not disburb my application archetecture. Other wise I have to choose other alternative.