Introduction:
In this article, I will explain how to extend the DataGrid class to provide some new functionalities. Building new web controls from scratch is not the best solution in most of cases, just adding some new functionalities to existing .NET Web controls will save you time And Provide More Stability. in Addition, They'll Be More Compatible with any new version of .NET FRAMEWORK.
FunctionAgities:
Add The Total Number of Rows in The DataGrid Footer. Add Serial Column
Source Code:
Let's Explain The Code Now Step by Step, First We Have To Create The Class That Will Extend The DataGrid Class:
using System; using System.Web.UI; using System.Web.UI.WebControls; using System.ComponentModel; using System.Collections; using System.Data; [assembly: TagPrefix ( "BNaffa.Web.Ui.WebControls", " ExtendedDG ")] namespace BNaffa.Web.UI.WebControls {[DefaultProperty (" ShowNoRows "), ToolboxData (" <{0}: ExtendedDG runat = server> {0}: ExtendedDG> ")] public class ExtendedDG: DataGrid {// strings constants represent the view state indexes const string SHOW_NO_ROWS = "ShowNoRows"; const string NO_ROWS = "NoOfRows"; const string SHOW_SERIAL = "ShowSerial"; // Constructor public ExtendedDG (): base () {} // Properties & Methods go here}
THEN, WE'LL Add The Following TWO Properties:
Shownorows [true / false] showserial [true / false]
[Bindable (true), Category ( "Appearance")] public bool ShowNoRows {get {return Convert.ToBoolean (ViewState [SHOW_NO_ROWS]);} set {ViewState [SHOW_NO_ROWS] = value;}} [Bindable (true), Category ( "Appearance")] public bool ShowSerial {get {return Convert.ToBoolean (ViewState [SHOW_SERIAL]);} set {ViewState [SHOW_SERIAL] = value;}} This private function will check if we are in the design mode or in runtime mode :
Private bool isindesignmode () {if (this.site! = null) Return this.site.desiGnmode; return false;}
Total Number of Rows FunctionAlity:
NOW WAWANT TO KNOW THE? OK, HOW Do We get That? One of Solutions Is To Cast The Data Source Property for The Data Grid To a Data Set or Data Table and Get The NumBer of Rows. But I don't prefer this Way !! i will give you a short way without casting cost.
We Well Override The Method CreateColumnet Which Be Called When You Invoke The Method Database:
protected override ArrayList CreateColumnSet (PagedDataSource dataSource, bool useDataSource) {if (! dataSource = null) ViewState [NO_ROWS] = dataSource.DataSourceCount; return base.CreateColumnSet (dataSource, useDataSource);} As you see, this function will receive an object of TYPE PagedDataSource, this Object Contains a DataSourceCount Property,
THEN WEWSTATE COLLECTION. Using this method, you can get the data source count welch casting and with with any type of data sources [DataSet, DataView, DataTable, ArrayList, etc ...]
Now We Will Override The Method OnItemcreated, To Add The Total Number of Rows In The DataGrid Footer:
protected override void OnItemCreated (DataGridItemEventArgs e) {base.OnItemCreated (e); if // Only in Runtime Mode {if (e.Item.ItemType == ListItemType.Footer) {if (ShowNoRows && ShowFooter) (IsInDesignMode ()!) {IF (E.Item.cells.count> 0) {E.Item.cells [0] .text = viewstate [no_rows] "rows";}}}}}
Serial Column FunctionAlity:
Before Writing The Serial Number To The DataGrid, We Have To Add A Template Column for the Serial Field, So Weme This Class Which Implements The Interface Itemplate and The Method Instantiatein:
Public class mycolumn: Itemplate {public void instantiatein (control container) {}} we will add this serial column in the ononit method meth handles The DataGrid Initiation Process:
protected override void OnInit (EventArgs e) {base.OnInit (e); if // Only in Runtime Mode {TemplateColumn tmpCol = new TemplateColumn () (IsInDesignMode ()!); MyColumn myCol = new MyColumn (); tmpCol.ItemTemplate = MYCOL; this.columns.add (tmpcol);}}
Now We Have To Add The Serials To The New Column, We Will Add Another Fragment Code To The Previous Overridden Method OnItemcreated, So It Will Be Like this:
protected override void OnItemCreated (DataGridItemEventArgs e) {base.OnItemCreated (e); if // Only in Runtime Mode {if (e.Item.ItemType == ListItemType.Footer) {if (ShowNoRows && ShowFooter) (IsInDesignMode ()!) {IF (E.Item.cells.count> 0) {E.Item.cells [0] .text = viewState [no_rows] "rows";}}} ==}}} == ListItemTYPE.HEADER ) {IF (showserial && showhead) {E.Item.cells [0] .text = "#";}} else if (E.Item.ItemType == Listi Temtype.Item || E.Item.itemType == ListItemType.alternatingItem) {if (showserial) {E.Item.cells [0] .text = (E.Item.ItemIndex (this.pageSize * this.currentpageIndex) 1) .ToString ();}}}} The important thing here is the equation that calculates the current row number, we will consider the current page index to work properly with DataGrid paging (e.Item.ItemIndex (this.PageSize * THIS.CURRENTPAGEINDEX) 1)
Now you can compile the project and use the assembly to add new control to your toolbox tab in Visual Studio.NET., And you can use the new DatarGrid and set these properties ShowNoSerial, ShowSerial to true to show this functionality.Download Source Code and Sample!