This little code tidbit makes it easy to insert a row into a
gridview bound to an
I hate... I mean hate placing and naming controls in the footer row of my
gridview so that I can do an insert. What I hate even more is looking up with findcontrol("controlname") all of the controls in the footer row, clearing the
sqlDatasources insert parameters, and then adding them back one at a time with the value of the control in the footer that I just created. It just seems ridiculous. So I'm working on a project this week that literally has 27
gridviews that need to allow an insert. Ok so I could use a
detailsview control but that's just way too time consuming and it does not look nice, and all I want to do is let the user type, select, or check mark a few values, click an insert link, and have the job done.
Yes I could put the fields in a table below the grid view, I know but I don't like that either. So here is the solution I came up with and I'm betting some of you will be just thrilled to have it. It took about 6 hours to figure out, but it takes about 5 minutes to implement now on each
gridview that I want to allow an insert for.
Using the Code
So let's start with our
gridview. This is a big nasty one to demonstrate the point, though a little two field one will work just as well. The first really important thing to notice is that every field has the
SortExpression set to the corresponding name of the filed in the
datasource. If you have to do some other kind of sorting or you don't want the user to be able to sort by all of the fields, this isn't going to work for you and you'll have to do it the long way... sorry.
Also notice that the
OnRowCommad is set,
true and that the
ID column is set to