brady gaster

yadnb

Empty GridView, Invisible Footer

My current project involves a few GridViews. We're going with the "add new row" technique, and placing the form for the new row in the footer of our GridView. So if the GridView has three columns, first, last, email, the footer would have three textboxes to collect that data with an add button or something. Got it?

Well, that's all well and good if you've already got data in your database or your GridView. If you don't you're totally screwed, because the footer won't even show up. I've seen a few suggestions on how to accomplish this thing, but none of them really appealed to me. One suggested that your stored proc always return a null row. Not going to happen in any real-world shop, of course, so its up to your code to accomplish this amazing feat. In my situation I'm using ObjectDataSources and DataSets, so I have a moderately agreeable solution.

Create an Event Handler for the ObjectDataSource's Selected event.
Within that handler, add a new record to your source. In this way, you've added a row to the local data store store just prior to binding your GridView. Since the GridView now has a row in it, the footer (and header) will show up. Hoakie though it is, it solves the problem created by an even hoakier decision made on the part of MS's control team - to hide your header and footer if you have no data. 

protected void OnPeopleDataSourceSelected(object sender, ObjectDataSourceStatusEventArgs e)
{
MyDataSet.PeopleTable dt = e.ReturnValue as MyDataSet.PeopleTable;
if (dt.Rows.Count == 0)
{
MyDataSet.PeopleRow row = dt.NewPeopleRow();
row.First = DBNull.Value;
row.Last = DBNull.Value;
row.Email = DBNull.Value;
dt.Rows.Add(row);
}
}
Posted: Oct 24 2006, 09:47 AM by tatochip | with no comments
Filed under:

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required)