Video Transcript:
Hi, this is Ken from aspvideocasts.com. This simple sample example shows you the cleanest way, or at least the cleanest way I know, to nest Gridviews in ASP.NET 2.0. We’ll start with a blank project. We have our default page, and we’ll drag a Gridview to the page. This is going to be our outside Gridview, so I’ll change the name to “Gridview Outside” and using the wizard I’ll create and bind to a data source. I’ll tag that with “outside” as well. I’ll create a new connection, type in my password. I already have a database created, and that tests out OK. And my outside Gridview is going to display my orders. I’ll select the order ID and customer name. And at this point if I run the project I should see the outside Gridview display the order ID and the customer name. OK as we can see, that worked.
Now back into our project, we’ll create a template column. This template column is going to hold our inner, or inside, or nested Gridview, and it’s going to display our order items. So I’ll edit a template. I’ll grab another Gridview and drag it into that template column. This one I’m going to call “Gridview inside.” Again I’ll use the wizard to create and bind to a data source. This time I want my order items. And really all I need to show is my item name. But I want to put a clause in here, a sql statement, where my order ID equals. So I simply added a sql variable to that query. And I’ll end my editing here of the template. Now here’s where the fun begins. We have to go to the codebehind.
So you can see from the codebehind I’ve already pasted in some code here to save time. This is an event for our outside Gridview called the row databound event. And what that does is it passes us in a variable called “e” the row information. So what we’re going to do is inside this subroutine we’re going to define My STS, which is a sql data source, and we’re going to make it equal, the sql data source for the inside for that particular row when it’s being bound. Then what it’s going to do is set the parameter, remember we created that sql variable, the first parameter being zero. And we’re going to set the default value. So this parameter is the order ID. We’re going to set that value to the outside Gridview’s databound item called order ID, order ID being the name of the database field. We go back to the front side of our page and we run this.
We should now see a Gridview. This is what we had before, the order ID displayed, the customer name displayed. But now for each particular row, we can show the items ordered from that order. So as we can see, Bob Smith ordered a hard drive, video card and memory stick. Jim Bob ordered a monitor and printer, and so on and so forth. So this concludes this simple sample example. The source code as always can be found at aspvideocast.com. Please be sure to check back as new examples will be posted. And you can use the feedback form to request a simple sample example. Until next time, this is Ken at aspvideocast.com checking out.