Why does GridView reset its values to what it was before user edits?

Why does GridView reset its values to what it was before user edits?

If I bind GridView (via DataSourceID attribute) to SqlDataSource and set SelectCommand and UpdateCommand attributes, then everything works perfectly.

But if we manually call GridView.DataBind inside *Page_Load()*, then SqlDataSource doesn’t perform any updates, even though SqlDataSource.Updating and SqlDataSource.Updated events do fire when GridView’s Update button is clicked. I think this is due to the fact that GridView resets to what it was before the user edits:

a) Why does GridView reset its values if we manually call DataBind() inside *Page_Load()*?

b) Since Update operation doesn’t work when manually calling DataBind, I would then assume that Delete operation also wouldn’t work, but it does. Why?


DataBinding in a List View


Data Binding as a design pattern
I believe Page_Load runs before your changes take place, thus you bind the old data before you run the updates.
ObjectDataSource created twice when control is changed
Wrap the bind in a If Not IsPostBack when under Page_Load, i believe that will fix your problem.

Databind List of Integers
Is it possible to 'refresh' WPF data bindings

OneWayToSource binding from readonly property in XAML

Forms Databinding - DataSet not updating?


WPF Context menu doesn't bind to right databound item
You will need to assign the GridView with source and also databind it.

Something along the lines of the following code: .
Page_Load   if(!Page.IsPostBack)  {     gv1.DataSource = GetData();      gv1.DataBind();  } 

82 out of 100 based on 42 user ratings 1342 reviews