DataBinding in ASP.NET 2.0 and the RowUpdating event

For a while now I’m trying to figure out why my method, triggered by the GridView.RowUpdating event, doesn’t work as all samples say it should do. All samples of course assume you’re doing everything in your .aspx page, but I have to do everything in my code-behind, because on forehand I don’t know what I’ll be binding to my GridView. Check out what I’m trying to do, maybe you can help?

First, I load up some data:

DataSet ds = new DataSet(); SqlConnection con = new SqlConnection(“somestring”); SqlCommand cmd = new SqlCommand(“select * from sometable”, con); SqlDataAdapter adap = new SqlDataAdapter(cmd); adap.Fill(ds);

After that, I’m doing some work to make my GridView look beautifull, adding all columns by hand. But for this demo, I’ll show you the simple version, that also doesn’t work 🙂
Let’s bind the data to our GridView and have it autogenerate our columns, as well as an edit button.

GridView1.AutoGenerateColumns = true; GridView1.AutoGenerateEditButton = true; GridView1.DataSource = ds; GridView1.DataBind();

Now I’ve tried a lot of places to setup subscription to the events. Currently I’m in the OnInit method, but it doesn’t matter where you place this code.

protected override void OnInit(EventArgs e) {   base.OnInit(e);   GridView1.RowUpdating += new GridViewUpdateEventHandler(GridView1_RowUpdating);   GridView1.RowUpdated += new GridViewUpdatedEventHandler(GridView1_RowUpdated);   GridView1.RowEditing += new GridViewEditEventHandler(GridView1_RowEditing);    }

So now everything’s setup, let’s create a method for the RowEditing event.

void GridView1_RowEditing(object sender, GridViewEditEventArgs e) {   GridView1.EditIndex = e.NewEditIndex;   GridView1.DataBind(); }

Been there, done that… Okay, now the part that doesn’t work!

void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e) {   // This method is never even touched! } void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) {   // e.Keys.Count == 0   // e.NewValues.Count == 0   // e.OldValues.Count == 0 }

When we edit a row in our GridView and press the “Update” button, at some time it’s received in the RowUpdating method. But as I noted in the comments in that method, some collections that should contain the columns (names, old values and new values) are always empty. Always. And the RowUpdated method is never even touched!!!

Okay, so I’ve read on the ASP.NET Forums that I need to use a DataSource control. For example a SqlDataSource, which is automatically added to your WebForm if you drag-n-drop your way around Visual Studio 2005.The problem is, I’d very much like to do so, if ASP.NET 2.0 requires this. But I can’t set a DataSource property or anything on the SqlDataSource!!!

So if you have any solution to my problem… I probably have to read the cells on the GridView of the selected row, find the controls, get the values from those and insert those into my DataSet. Which means I won’t make my 70% code reduction Microsoft has always promised me. Bah!