Introduction

DataGrid control is the main method to display information for web users. Heavy loaded applications like accounting, stock management, etc. require many more reports and update while application gets bigger. Altering the data source, reflecting the column changes to the datagrid mostly takes the longest time. Moreover displayed data of a report can vary for each branch or user, end-user requests for summary columns, column positions, cell colors, etc. is sometimes frustrating. End-user doesn't want complicated grid controls, so we have degraded our complicated grid controls to standard datagrids..

Background

Basically this method dynamically controls the aspx datagrid content from a stored procedure. If you alter the datasource columns, you don't need to update the project files, and you may control display format of the grid from the same data source.

Using the Code

First of all, we must provide a datasource for datagrid. I'm using SQL Server 2005 for my projects so the samples work on that platform. SQL command below fetches the "deliveries" table's data with given criteria.

First argument of the command between "[]" characters must be the name of the column. Column name can be different from field name of the table. And then we're concatenating optional remaining format arguments separating by "|" character.

"company_title" table field is being named for datagrid as "title". "caption" parameter provides column header text and caption of this column will be "Company Title". "alignment" argument can take one of the "left","right" or "center" parameters. You may get other arguments and their proper usage from the below list:

GridFormatter object will be initialized in code behind with the datatable that contains the stored procedure generated data. Formatted rows and columns will be applied to the datagrid in the RowDataBound event.

GridFormatter Object

GridFormatter object provides formatted data and click event behaviours. All the data source commands will be handled in this object. Please refer to the command abbreviations and usages in GridFormatter's ColumnData class.

"HandleRowDataBoundEvent" method is being called from the RowDataBound event of aspx page. Format commands will be applied to the grid rows in this event.

If a postback is requested when a row is clicked on datagrid EventType parameter of the HandleRowDataBoundEvent method must be set to GridFormatter.EventTypes.ServerSide. So, gvMain_SelectedIndexChanged event will be fired when user clicks on a row.

If you want to catch the click event on an image button without postback EventType parameter must be GridFormatter.EventTypes.ClientSide. Event must be controlled by a JavaScript function which is called GridCellClicked() in aspx page.

Summary Row

If you specified a "sum" argument in a table field as an alias, you must add the following command after the DataBind() method. This method adds footer row at the end of the datagrid, and will show the total of the numeric columns that has "sum" argument. Display format of the summary cell will be the same as the column format.

gf.SetSummaryColumns(refthis.gvMain);

Points of Interest

The control must have a paging function. I'm using an algorithm for that, but it's not effective for some reason. Another todo list item is packing it in a user control of course.

Conclusion

I hope this code snippet will help you if you are lost in reports like me. Any new ideas and fixes are welcome...

Comments and Discussions

If you want formatting to be dynamic you could also define it separately and parse the formatting in C# code to display your data. Then you can test it without executing a stored proc and your data and presentation are separate.