views:

793

answers:

2

1) Suppose GridView1 is bound to ObjectDataSource and that GridView1.SelectedIndex is set to value 7. If we then rebind GridView1 by calling GridView1.DataBind(), GridView.SelectedIndex will still retain its initial value ( 7 ).

Are there any other properties which GridView remembers across rebinding operations?

2) Suppose GridView1 is bound to ObjectDataSource control. When GridView1’s row gets deleted, we can call GridView1.DataBind() inside GridView1.RowDeleted event handler to retrieve values from data source. If we don’t call Gridview1.DataBind inside that event handler, then DataBind() will automatically be called by Asp.Net and the result will be the same.

So are there any reasons why we should manually call GridView.DataBind( inside RowDeleted event handler ) instead of letting Asp.Net call it automatically?

A: 

Your answer lies in Page.DataBind and Control.DataBind methods. The main difference is that all data sources are bound to their server controls after the Page.DataBind method is called. No data is rendered to the control until you explicitly call either the DataBind method of the Web server control or until you invoke the page-level Page.DataBind method. Typically, Page.DataBind (or DataBind) is called from the Page_Load event. Source

Particular answer for 1) is there's no such property for GridView,but you can make your own one and add last changed control.(Ideal)

For 2)Your action here coming from a postback,because the default bound source is changed after all you deleted a row inside it,since default source cannot be bound again,you need to call DataBind manually.

Myra
1)You mean there is no such property besides GridView.SelectedIndex property, which as far as I know does remember its value across rebindings?2)Are you saying that after Delete operation is performed, GridView won’t be automatically rebinded? I checked this and gridView is automatically rebinded!
SourceC
Typical values contained in GridView are:SelectedValueSelectedRowValueSelectedRow.DataItem (Item in DataSet)After delete operation your source stays same,but view is not By Default , source is bound but as a view,not the source itselfYou may know this issue if you used DataSet,DataTable and DataView object containers.
Myra
Uhm, I’m not following you. I realize that data source is bound to DataView and not DataTable itself, but I don’t see how that is relevant to our discussion
SourceC
I've just given an example for relations on databinding.You realize this relations in a wrong manner.Tell me how unclear you are so i can tell you in your way.
Myra
I apologize for any inconveniences, but I’m a bit on the slow side, so you will have to come down to my level, since your terminology (and the structure of your sentances) is confusing and open to interpretation For starters, I have no idea what you mean by “relations on databinding” and what you were trying to convey in your message before last one. Second, I still have no idea whether there are any reasons why or when we should manually call GridView.DataBind( inside RowDeleted event handler ) instead of letting Asp.Net call it automatically
SourceC
A: 

I'm going to mention a bit about Hierarchical Data Binding for starters.
In order to bind a datasource to a server-side control (GridView,DetailsView,.. etc) , that control must support a property known as DataSource and a method known as DataBind() , and the datasource that control is bound implement IEnumerable interface.

There is an exception for DataSet and DataTable which can be both bound directly , which results in binding to the default DataView of default table.

To bind a data to a control , you assign the data source to the DataSource property of the control and call its DataBind() method.Source

Since in your case ,databinding situation is different as you mentioned earlier that you bind GridView with ObjectDataSource which executes a query in page load and in every SELECT,INSERT,UPDATE,DELETE operations,datasource is bound automatically (which means DataBind is also called automatically).

There is another case which you are looking for an answer in the first place is that if datasource is queried and bound in page events (i.e. you query something and get a result with DataTable and bind it into GridView --> this source is not in the page not a objectdatasource or something else stands on page as static source).Since this query is coming from an event,when you enable pageIndex you must query it again that leads to you must also bind it with DataBind.

DataTable sourceTable = GetDataFromServer();
gridView.DataSource = sourceTable;
gridView.DataBind();

I hope that you can understand this time.

Myra