Its been a long time I have written any article on ASP.NET. Just few weeks back I got a very interesting requirement where I had to customize my GridView by Grouping the Columns and also merging the Rows by using Colspan properties. So while in View mode of GridView the data should be display in Rows and Columns format, but when I click on Edit it will display in List Format where we can place the controls in any format we want. You can refer to the figure below for the final target what I am going to show in this article.
In the figure above you can see the Employee Details and Address Details are grouped together. Also in Edit Mode the same data is displayed in List Format where we are free to put the controls in any format. I have used different colours for different groups so that groups of similar items can be easily identifiable in any format.
To keep simple I am using SqlDataSource control for databinding and Employee table of Northwind database. Assuming that you must be aware of the basics of databinding and asp.net I am going directly to the topic. This topic covers mostly the control properties of ASP.NET so you may connect to any datasource or databinding controls as per your requirement.
Before I start I have done the following steps
- Created a empty ASP.NET web application.
- In default.asp page added a GridView control and a SqlDataSource.
- Configured the SqlDataSource to connect to SqlServer Northwind Database.
- Selected Employee table from the Database, and from the Advanced property of the Configuration screen of SqlDataSource I have selected to Generate Insert, Update and Delete Statement.
Refer the figure below for the specific configuration of SqlDataSource mentioned above.
Once you save these changes you can select to Bind the GridView with the SqlDataSource. Now configure the GridColumns by Selecting the EditColumns as given in the screen below.
This will open the popup where you can customize the individual columns, to customize the columns as per our requirement convert all the fields into TemplateFields which you can do from the same popup screen.
Once these changes are done, Our configuration part is done and now we are ready to move to the coding part where each and every steps will define the look and feel of our required screen.
First of all I am going to show my default.aspx page, while doing the steps above my VS2010 tools have generated lots of code. I am going to clean up the codes which I dont want at this movement and which is out of scope of this article. If you can see the code below I have cleared the InsertParameter, DeleteParameter, InsertCommand and DeleteCommand codes from SqlDataSource. The only part right now I am interested in is SelectCommand, UpdateCommand and UpdateParameters. And leaving rest of the codes untouched.
Now let me run my program, this gives me the following output. This is just the default layout which my asp.net page gave me.
So let me start with the customization. First I am going to merge the Columns. To get this I have to write just simple few lines of code on RowCreated event of my Employee GridView.
This code is referring to the method AddMergedCells with GridView, TableCell, number of Columns to merge, Title of the Merged Cells with the background color as an argument. In the code above first I took the instance of the Current GridView with GridView row oblect and TableCell object. And with these empty cells we finally created the Merged cells and added to the top of the current instance of the GridView.
In AddMergedCells, we have just few lines of code which basically sets the different properties of the TableCells and using the ColSpan it creates the Merged cells and add to the GridView. Now once you run the code ideally we should get the following output.
As you can see above the same grid is little decorated now, with merged columns. This takes us one step closer to our requirement. Now our next target is to Create the Merged cells. To get this first I have created a simple table with all the columns in the custom format which I wish to display, alternatively you can use any other layout too. Code of which is given below.
Now coming to the rest of the code you have to remove all the EditItemTemplate codes from all the columns except the first Column EmployeeID, cause this column is now going to span over rest of the Columns. Next paste the above table layout format into the EditItemTemplate of the EmployeeID. Which will give you the code as below. This is also our complete code of the ASPX page.
Now since the first columns are spanning into the rest of the Columns, so we have to write few lines of code to hide the rest of the columns. So here in the RowDataBound of EmployeeGrid, I have hidden rest of the columns except the Last column where we have kept our Command Buttons and the first Column where basically our code sits which spans in the other columns.
Now lets run the example code, so we are almost done now, as you can see in the screen below this is exactly what we were expecting. But just wait a moment, this are just the visual layout what about saving the modified data?
Ok, now first thing we have to do is to rename all the controls properly so that we can refer the controls in the FindControl methods in code behind. And now I have to capture the Updating event of the SqlDataSource, where basically I will get the reference of the input controls with the modified data if any and set the DefaultValue of the UpdateParameters of SqlDataSource control.
So with this I have completed my end to end databinding, column merging, row merging, updating our grid view control using SqlDataSource. In this example I have used Northwind database which you can get online. And this example is compatible with ASP.NET 2.0 and above.
On top of this you can customize this according to your requirements, like changing the layout in some other format, add images, other controls, adding validation controls, Inserting new record, deletion, etc.