in

ASP.NET Weblogs

Jason Tucker's Blog

not at all creative...

GridView is useless OR I can't figure this out.

So I’m happily coding away converting a legacy system ( not really legacy I guess, ASP.Net 1.1) and get to a part where I can add some new functionality to the site. I decide to update to the GridView and check things out. CommandField columns are a god send, or so I thought.

So like most real apps out there we all have some sort of DataBind() function in our libraries to preform calls to BLL or DAL’s depending on how things go. So I wire all this up, add some CommandField columns for editing, updating and deleting and start to run through the new part.

First error I get is when I try to cancel the in line edit function. I get an exception thrown stating that I hadn’t wired up the RowCancelingEdit Event. Fine, I don’t see the point but okay I’ll do it. Really at most I set the editindex to -1 and rebind the data. I don’t get why the framework can’t just do that for me only with ViewState data. Anyway, I wire that up. Now comes the huge pain in the ass. I have my RowUpdating event wired up and I notice that the GridViewDeleteEventArgs have concurrency built in with the OldValues and NewValues collection. Sweet! I can get the new value set my BLL value to the new value and hit save and be done. 10 lines of code cut down to 4, fantastic!

If only it was that easy. Instead I get nothing from these collections. Nothing at all. I’ve tried every combo of attributes on the GridView and CommandField column with none of them working. So I did some digging, I couldn’t find any real good examples of people actually databinding the GridView without using the declartive DataSources. I did happen to stumble across a post on the beta forums about this and the result was that I had to find the TextBox control that was generated an get the value that way. If this is so, I don’t see the value in using the GridView at all and will happily switch back to the old busted way. I’d really prefer the new hotness way though.

So if anyone can shed some light on this and hopefully prove me to be just lazy and not very good at google then that would be great.

Published Aug 23 2005, 07:57 PM by jtucker
Filed under:

Comments

 

Polita Paulus (ASP.NET Team) said:

Hi Jason-

So glad to hear you're using GridView! GridView was designed to work best with declarative data source controls, such as SqlDataSource and ObjectDataSource. When declarative data sources are used, we know how to retrieve the data and can call DataBind() at the right times so you don't have to. If you're using the V1-style databinding (ie, binding to the DataSource property rather than the DataSourceID property), we don't know how to retrieve your data, so we can't call DataBind() for you. For instance, some people will call a method to retrieve their data. Others will bind to a property on the page. Others will retrieve their data from cache, session state, or view state. There's no limit to where that data can come from. Because we can't set the DataSource property and call DataBind() for you, there's little use in setting properties such as EditIndex in event handlers, since you may need to do some operations before EditIndex is set, and you will have to retrieve your data, set DataSource, and call DataBind() afterwards. The result is that we could not improve the V1-style databinding model without taking away the flexibility that users loved so much.

To reap the advantages of GridView, DetailsView, and FormView, data source controls offer a simple, declarative way to access and control your data. If you have existing code that returns your data that you would like to use, the ObjectDataSource is for you. If you are doing simple TSql database access, SqlDataSource is a good way to go. There are several other data sources in Whidbey, and if you don't find what you need, they are pretty straightforward to write.

If you decide not to go with the new data source control model, GridView will work much the same way that DataGrid did, offering you a high level of flexibility and the familiarity of DataGrid-style binding.

You can read more about using GridView with data source controls here: http://beta.asp.net/QuickStartv20/aspnet/doc/ctrlref/data/gridview.aspx.
August 24, 2005 2:41 PM
 

bum said:

I know this is a very old post, but still very relevant today..

Polita, it is a mere oversight and lack of attention that the GridView argument does not include old and new values. I wrote generic code that pulls out all the values, so why can't MS write such code?

It is simple, only one loop that loops through the current row's cells and uses the ExtractValuesFromCell method to get all the values out. In fact, even the template column values can be extracted as long as the control inside the template uses the "Bind" keyword.

Honestly, I beleive this is an oversight on MS's part to ship something incomplete. But thats what keeps the third party component vendors in business.

July 30, 2007 1:07 PM
 

chris said:

bum, what do you mean using the "Bind" keyword?  I am trying to do exactly what you allude to, namely, retrieve values from a template column via Extraction.

August 23, 2007 1:26 PM

Leave a Comment

(required)  
(optional)
(required)  
Add