using System; using System.Collections.Generic; using System.ComponentModel; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Collections; using System.Collections.Specialized; using System.Web.UI.HtmlControls; namespace Gui.Controls { [DefaultProperty ( "Text" )] [ToolboxData ( "<{0}:XGrid runat=server></{0}:XGrid>" )] public class XGrid : GridView { #region Constructor /// <summary> /// Initialises an <see cref="XGrid"/>. /// </summary> public XGrid () { } #endregion #region Properties #region HasCustomFormatting /// <summary> /// Whether the results summary should be shown. /// </summary> [DefaultValue ( true )] [Category ( "Appearance" )] [Description ( "Whether this gridview would need custom formatting per column ." )] public bool HasCustomFormatting { get { if (HttpContext.Current.Session["global." + this.ID + ".HasCustomFormatting"] == null) return false; else return (bool)HttpContext.Current.Session["global." + this.ID + ".HasCustomFormatting"]; } set { HttpContext.Current.Session["global." + this.ID + ".HasCustomFormatting"] = value; } } #endregion #region Ld private ListDictionary ld; [Description ( "Contains the extended properties values from the stored procedures " )] public ListDictionary Ld { get { return ld; } set { this.ld = value; } } #endregion Ld #region MaxCharColapse /// <summary> /// Whether the results summary should be shown. /// </summary> [DefaultValue ( 50 )] [Category ( "Appearance" )] [Description ( "At which character the column of the gridview should colapse" )] public int MaxCharColapse { get { if (HttpContext.Current.Session["global." + this.ID + "MaxCharColapse"] == null) return 50; else return (int)HttpContext.Current.Session["global." + this.ID + "MaxCharColapse"]; } set { HttpContext.Current.Session["global." + this.ID + "MaxCharColapse"] = value; } } #endregion MaxCharColapse private int GvPageSize { get { if (HttpContext.Current.Session["global." + this.ID + ".GvPageSize"] == null) { //Utils.Debugger.WriteIf ( "GvPageSize is null " ); return 20; } //this is the default size of the page else { //Utils.Debugger.WriteIf ( "GvPageSize is " + System.Convert.ToString ( HttpContext.Current.Session ["global." + this.ID + ".GvPageSize" ] ) ); return System.Convert.ToInt32 ( HttpContext.Current.Session["global." + this.ID + ".GvPageSize"] ); } } //eof get set { try { //accept only whole numbers , if not whole number set default 7 int pageSizeToSet = value; HttpContext.Current.Session["global." + this.ID + ".GvPageSize"] = pageSizeToSet; System.Convert.ToInt64 ( pageSizeToSet ); if (pageSizeToSet >= this.DataSourceCount || pageSizeToSet < 0) { HttpContext.Current.Session["global." + this.ID + ".GvPageSize"] = this.DataSourceCount; //Utils.Debugger.WriteIf ( "The DataSourceCount is " + System.Convert.ToString ( this.DataSourceCount ) ); this.MyPageIndex = 0; //variant Unable to cast object of type 'System.Web.UI.WebControls.TableRow' to type 'System.Web.UI.WebControls.GridViewRow //this.AllowPaging = false; } //eof to return all if else { HttpContext.Current.Session["global." + this.ID + ".GvPageSize"] = value; } //eof if the desired page size is smaller than the number } //eof try catch (System.FormatException sfe) { HttpContext.Current.Session["global." + this.ID + ".GvPageSize"] = 13; //fatal number of exceptions } //eof catch } //eof set } //eof property GvPageSize private string GetSortDirection () { switch (GridViewSortDirection) { case "ASC": GridViewSortDirection = "DESC"; break; case "DESC": GridViewSortDirection = "ASC"; break; } return GridViewSortDirection; } #region GridViewSortDirection private string GridViewSortDirection { get { return HttpContext.Current.Session["global." + this.ID + "SortDirection"] as string ?? "ASC"; } set { HttpContext.Current.Session["global." + this.ID + "SortDirection"] = value; } } #endregion GridViewSortDirection //private GenApp.Bo.User userObj; //public GenApp.Bo.User UserObj { get { return this.userObj; } set { this.userObj = value; } } #region GridViewSortExpression private string GridViewSortExpression { get { return HttpContext.Current.Session["global." + this.ID + "SortExpression"] as string ?? string.Empty; } set { HttpContext.Current.Session["global." + this.ID + "SortExpression"] = value; } } #endregion //GridViewSortExpression #region MyPageIndex public int MyPageIndex { get { // //Utils.Debugger.WriteIf ( "The page index of gb with id " + Convert.ToString ( // HttpContext.Current.Session ["global." + this.ID ]) + " is " + //Convert.ToString ( // HttpContext.Current.Session ["global." + this.ID + "MyPageIndex"] )) ; return System.Convert.ToInt16 ( HttpContext.Current.Session["global." + this.ID + "MyPageIndex"] ); } //eof get set { this.PageIndex = value; HttpContext.Current.Session["global." + this.ID + "MyPageIndex"] = value; } } //eof property MyPageIndex #endregion //GridViewSortExpression #region StrLabMsgText private string _StrLabMsgText; public string StrLabMsgText { get { // //Utils.Debugger.WriteIf ( "The page index of gb with id " + Convert.ToString ( // HttpContext.Current.Session ["global." + this.ID ]) + " is " + //Convert.ToString ( // HttpContext.Current.Session ["global." + this.ID + "StrLabMsgText"] )) ; return System.Convert.ToString ( HttpContext.Current.Session["global." + this.ID + "StrLabMsgText"] ); } //eof get set { _StrLabMsgText = value; HttpContext.Current.Session["global." + this.ID + "StrLabMsgText"] = value; } } //eof property StrLabMsgText #endregion #region DataSourceCount private int _dataSourceCount; /// <summary> /// The total number of rows in the data source. /// </summary> [Browsable ( false )] public int DataSourceCount { get { if (this.Rows.Count == 0) return 0; else if (this.AllowPaging) return this._dataSourceCount; else return this.Rows.Count; } set { this._dataSourceCount = value; } //eof set } #endregion #region ColumnNumberToGenerateLinksTo private int _ColumnNumberToGenerateLinksTo; /// <summary> /// The total number of rows in the data source. /// </summary> [Browsable ( false )] public int ColumnNumberToGenerateLinksTo { get { if (_ColumnNumberToGenerateLinksTo == null) return 0; else return _ColumnNumberToGenerateLinksTo; } set { this._ColumnNumberToGenerateLinksTo = value; } //eof set } #endregion #region UrlToLinkTo private string _UrlToLinkTo; /// <summary> /// Whether the results summary should be shown. /// </summary> [DefaultValue ( true )] [Category ( "Appearance" )] [Description ( " Which is the link to link to." )] public string UrlToLinkTo { get { if (HttpContext.Current.Session["global." + this.ID + ".UrlToLinkTo"] == null) return String.Empty; else return (string)HttpContext.Current.Session["global." + this.ID + ".UrlToLinkTo"]; } set { HttpContext.Current.Session["global." + this.ID + ".UrlToLinkTo"] = value; } } #endregion //#region ColNameToLinkAgainst //private string _ColNameToLinkAgainst; ///// <summary> ///// Whether the results summary should be shown. ///// </summary> //[DefaultValue ( true )] //[Category ( "Appearance" )] //[Description ( " Which is the Column Name to link Against." )] //public string ColNameToLinkAgainst //{ // get // { // if (HttpContext.Current.Session["global." + this.ID + ".ColNameToLinkAgainst"] == null) // return String.Empty; // else // return (string)HttpContext.Current.Session["global." + this.ID + ".ColNameToLinkAgainst"]; // } // set { HttpContext.Current.Session["global." + this.ID + ".ColNameToLinkAgainst"] = value; } //} //#endregion #region ShowResultSummary /// <summary> /// Whether the results summary should be shown. /// </summary> [DefaultValue ( true )] [Category ( "Appearance" )] [Description ( "Whether the results summary should be shown." )] public bool ShowResultSummary { get { if (HttpContext.Current.Session["global." + this.ID + ".ShowResultSummary"] == null) return false; else return (bool)HttpContext.Current.Session["global." + this.ID + ".ShowResultSummary"]; } set { HttpContext.Current.Session["global." + this.ID + ".ShowResultSummary"] = value; } } #endregion #region AllowInserting /// <summary> /// Whether the GridView will allow rows to be inserted. /// </summary> [DefaultValue ( false )] [Category ( "Behavior" )] [Description ( "Whether the GridView will allow rows to be inserted." )] public bool AllowInserting { get { if (HttpContext.Current.Session["AllowInserting"] == null) return false; else return (bool)HttpContext.Current.Session["AllowInserting"]; } set { if (this.AllowInserting != value) this.RequiresDataBinding = true; HttpContext.Current.Session["AllowInserting"] = value; } } #endregion #region InsertRowActive /// <summary> /// Whether the insert row is active. /// </summary> [DefaultValue ( true )] [Category ( "Appearance" )] [Description ( "Whether the insert row is active." )] public bool InsertRowActive { get { if (HttpContext.Current.Session["InsertRowActive"] == null) return true; return (bool)HttpContext.Current.Session["InsertRowActive"]; } set { if (this.InsertRowActive != value) this.RequiresDataBinding = true; HttpContext.Current.Session["InsertRowActive"] = value; } } #endregion #region InsertRow private GridViewRow _insertRow; /// <summary> /// The row in the grid that contains the insert controls. /// </summary> [Browsable ( false )] public GridViewRow InsertRow { get { return this._insertRow; } } #endregion #region HeaderRow private GridViewRow _myHeaderRow; /// <summary> /// Gets the row in the grid that corresponds to the header. /// </summary> [Browsable ( false )] public override GridViewRow HeaderRow { get { if (this._myHeaderRow != null) return this._myHeaderRow; else return base.HeaderRow; } } #endregion #region MyDataSource private DataTable _MyDataSource; public DataTable MyDataSource { get { return (DataTable)( HttpContext.Current.Session["global." + this.ID + "MyDataSource"] ); } //eof get set { this._MyDataSource = value; HttpContext.Current.Session["global." + this.ID + "MyDataSource"] = value; } } //eof property MyDataSource #endregion MyDataSource #region AscendingImageUrl /// <summary> /// Image that is displayed when <see cref="SortDirection"/> is ascending. /// </summary> //[Editor ( typeof ( ImageUrlEditor ), typeof ( UITypeEditor ) )] [Description ( "Image that is displayed when SortDirection is ascending." )] [Category ( "Appearance" )] public string AscendingImageUrl { get { return HttpContext.Current.Session["AscendingImageUrl"] as string; } set { HttpContext.Current.Session["AscendingImageUrl"] = value; } } #endregion #region DescendingImageUrl /// <summary> /// Image that is displayed when <see cref="SortDirection"/ /// is descending. /// </summary> //[Editor ( typeof ( ImageUrlEditor ), typeof ( UITypeEditor ) )] [Description ( "Image that is displayed when SortDirection is descending." )] [Category ( "Appearance" )] public string DescendingImageUrl { get { return HttpContext.Current.Session["DescendingImageUrl"] as string; } set { HttpContext.Current.Session["DescendingImageUrl"] = value; } } #endregion #region TextProperty [Bindable ( true )] [Category ( "Appearance" )] [DefaultValue ( "" )] [Localizable ( true )] public string Text { get { String s = (String)HttpContext.Current.Session["Text"]; return ( ( s == null ) ? "[" + "global." + this.ID + "]" : s ); } set { HttpContext.Current.Session["Text"] = value; } } #endregion Text #region FirstRowColor [Bindable ( true )] [Category ( "Appearance" )] [DefaultValue ( "" )] [Localizable ( true )] public string FirstRowColor { get { return (String)HttpContext.Current.Session["FirstRowColor"]; } set { HttpContext.Current.Session["FirsRowColor"] = value; } } #endregion FirsRowColor #endregion #region Instance members #region Events /// <summary> /// Fires before a row is inserted. /// </summary> [Category ( "Action" )] [Description ( "Fires before a row is inserted." )] public event EventHandler<GridViewInsertEventArgs> RowInserting; /// <summary> /// Fires after a row has been inserted. /// </summary> [Category ( "Action" )] [Description ( "Fires after a row has been inserted." )] public event EventHandler<GridViewInsertedEventArgs> RowInserted; /// <summary> /// Raises the <see cref="RowInserting"/> event. /// </summary> /// <param name="e">Event data.</param> protected virtual void OnRowInserting ( GridViewInsertEventArgs e ) { if (this.RowInserting != null) this.RowInserting ( this, e ); else if (!this.IsBoundUsingDataSourceID && !e.Cancel) throw new System.Web.HttpException ( string.Format ( "The GridView '{0}' fired event RowInserting which wasn't handled.", "global." + this.ID ) ); } /// <summary> /// Raises the <see cref="RowInserted"/> event. /// </summary> /// <param name="e">Event data.</param> protected virtual void OnRowInserted ( GridViewInsertedEventArgs e ) { if (this.RowInserted != null) this.RowInserted ( this, e ); } protected override void OnSorting ( GridViewSortEventArgs e ) { //Utils.Debugger.WriteIf ( "OnSorting START" ); GridViewSortExpression = e.SortExpression; //int pageIndex = this.PageIndex; int pageIndex = this.MyPageIndex; // this.DataBind ( ); //databind again after sorting //Utils.Debugger.WriteIf ( "From OnSorting" ); //this.PageSize = this.GvPageSize; //this.PageSize = 15; //variant this.PageIndex = pageIndex; this.MyPageIndex = pageIndex; //variant this.CreateBottomSummary ( ); this.DataSource = SortDataTable ( this.DataSource as DataTable, false ); this.DataBind (); //base.OnSorting ( e ); } protected override void OnDataBinding ( EventArgs e ) { //DEBUG//Utils.Debugger.WriteIf ( "OnDataBionding start " ); //int newSetPageSize = this.GetPagerSize ( ); //Utils.Debugger.WriteIf ( "From OnDataBinding" ); //this.GvPageSize = newSetPageSize; base.OnDataBinding ( e ); } //eof method OnDataBinding #endregion //W /// <summary> /// Create the pager. /// </summary> /// <param name="row">The pager row.</param> /// <param name="columnSpan">The number of columns the pager should span.</param> /// <param name="pagedDataSource">The data source.</param> protected override void InitializePager ( GridViewRow row, int columnSpan, PagedDataSource pagedDataSource ) { //DEBUG//Utils.Debugger.WriteIf ( "Initialize Pager START" ); // Nab total row count from data source #region GenericNumberPager #region Preparation this.DataSourceCount = pagedDataSource.DataSourceCount; ; HtmlGenericControl pageControls = new HtmlGenericControl ( "div" ); pageControls.Style.Add ( "max-width", "100%" ); pageControls.Style.Add ( HtmlTextWriterStyle.FontSize, "medium" ); // Create table cell for pager TableCell cell = new TableCell (); cell.ColumnSpan = columnSpan; cell.Controls.Clear (); //variant AddSomeFontStyle pageControls.Style.Add ( HtmlTextWriterStyle.FontFamily, "webdings" ); pageControls.Style.Add ( HtmlTextWriterStyle.FontSize, "medium" ); pageControls.Style.Add ( "float", "left" ); #endregion Preparation #region AddTheFirstLink LinkButton lnkFirst = new LinkButton (); lnkFirst.ID = "lnkFirst"; lnkFirst.ToolTip = "Click here to open the First page"; lnkFirst.CausesValidation = false; lnkFirst.Text = "First Page"; lnkFirst.CommandName = "Page"; lnkFirst.CommandArgument = "first"; //variant lnkFirst.Enabled = (this.PageIndex != 0); lnkFirst.Enabled = ( this.PageIndex != 0 ); pageControls.Controls.Add ( lnkFirst ); pageControls.Controls.Add ( new LiteralControl ( " " ) ); #endregion AddTheFirstLink #region AddThePreviousPageLink LinkButton lnkPrevious = new LinkButton (); lnkPrevious.ID = "lnkPrevious"; lnkPrevious.ToolTip = "Click to open the Previous page"; lnkPrevious.CausesValidation = false; lnkPrevious.Text = "Previous Page"; lnkPrevious.CommandName = "Page"; lnkPrevious.CommandArgument = "Prev"; lnkPrevious.Enabled = lnkFirst.Enabled; pageControls.Controls.Add ( lnkPrevious ); pageControls.Controls.Add ( new LiteralControl ( " " ) ); #endregion AddTheNextPageLink #region AddTheNumbersForPaging int intNumOfPages = System.Convert.ToInt16 ( this.DataSourceCount / this.PageSize ); int intCurrentPageIndex = this.MyPageIndex; int intSizeAdjuster = 0; int intPreviousTenPages = 0; int intNextTenPages = 0; bool flagDoCreatePagerLink = false; #region AddThePreviousTenPageLink intPreviousTenPages = intCurrentPageIndex - 10; if (intPreviousTenPages <= 0) intPreviousTenPages = 0; if (intPreviousTenPages != 0) { LinkButton lnkPreviousTen = new LinkButton (); lnkPreviousTen.ID = "lnkPreviousTen"; lnkPreviousTen.ToolTip = "Click to open the Previous 10 pages"; lnkPreviousTen.CausesValidation = false; lnkPreviousTen.Text = "-10..."; lnkPreviousTen.CommandName = "Page"; lnkPreviousTen.CommandArgument = intPreviousTenPages.ToString (); lnkPreviousTen.Enabled = lnkFirst.Enabled; pageControls.Controls.Add ( lnkPreviousTen ); pageControls.Controls.Add ( new LiteralControl ( " " ) ); } #endregion AddThePreviousTenPageLink #region IfThereIsOnlyOneItemItBehavesDifferently if (this.PageSize == 1) intSizeAdjuster = 0; else intSizeAdjuster = 1; #endregion IfThereIsOnlyOneItemItBehavesDifferently #region LoopTroughPageNumbers for (int i = 0; i < intNumOfPages + intSizeAdjuster; i++) { #region IfThePagerHasMoreThan2*10Pages if (intNumOfPages >= 2 * 10) { if (intCurrentPageIndex <= 10) { if (i <= intCurrentPageIndex + 10) flagDoCreatePagerLink = true; else flagDoCreatePagerLink = false; //else do nothing } //eof if ( intCurrentPageIndex <= 10) else if (intCurrentPageIndex <= intNumOfPages - 10) { if (i >= intCurrentPageIndex - 10 && i <= intCurrentPageIndex + 10) flagDoCreatePagerLink = true; else flagDoCreatePagerLink = false; } else if (intCurrentPageIndex > intNumOfPages - 10) { if (i <= intCurrentPageIndex && i >= intCurrentPageIndex - 10) flagDoCreatePagerLink = true; else if (i > intCurrentPageIndex) flagDoCreatePagerLink = true; else flagDoCreatePagerLink = false; } #region AddThePageNumberPagerLink if (flagDoCreatePagerLink == true) { LinkButton lbPager = new LinkButton (); lbPager.ID = i.ToString (); lbPager.CommandName = "Page"; lbPager.CausesValidation = false; lbPager.CommandArgument = ( i + 1 ).ToString (); lbPager.Text = ( i + 1 ).ToString (); //disable the links for the current page if (intCurrentPageIndex == i) { lbPager.ToolTip = "This is the current active page"; lbPager.Enabled = false; } pageControls.Controls.Add ( lbPager ); pageControls.Controls.Add ( new LiteralControl ( " " ) ); } #endregion AddThePageNumberPagerLink } //eof if (intNumOfPages > 20) #endregion IfThePagerHasMoreThan2*10Pages else { #region AddThePageNumberPagerLink LinkButton lbPager = new LinkButton (); lbPager.ID = i.ToString (); lbPager.CommandName = "Page"; lbPager.CausesValidation = false; lbPager.CommandArgument = ( i + 1 ).ToString (); lbPager.Text = ( i + 1 ).ToString (); //disable the links for the current page if (intCurrentPageIndex == i) { lbPager.ToolTip = "This is the current active page"; lbPager.Enabled = false; } pageControls.Controls.Add ( lbPager ); pageControls.Controls.Add ( new LiteralControl ( " " ) ); #endregion AddThePageNumberPagerLink } //else if ( intNumOfPages > 10 ) } //eof for #endregion LoopTroughPageNumbers #endregion AddTheNumbersForPaging #region AddTheNextPageLink LinkButton lnkNext = new LinkButton (); lnkNext.ID = "lnkNext"; lnkNext.ToolTip = "Next page"; lnkNext.CausesValidation = false; lnkNext.Text = "Next Page"; lnkNext.CommandName = "Page"; lnkNext.CommandArgument = "Next"; //variant lnkNext.Enabled = (this.PageIndex < (this.PageCount - 1)); lnkNext.Enabled = ( this.PageIndex < ( this.PageCount - 1 ) ); pageControls.Controls.Add ( lnkNext ); pageControls.Controls.Add ( new LiteralControl ( " " ) ); #endregion AddTheNextPageLink #region AddTheNextTenPagesLink intNextTenPages = intCurrentPageIndex + 10; if (intNextTenPages >= intNumOfPages) intNextTenPages = intNumOfPages; if (intNextTenPages != intNumOfPages) { LinkButton lnkNextTen = new LinkButton (); lnkNextTen.ID = "lnkNextTen"; lnkNextTen.ToolTip = "Next page"; lnkNextTen.CausesValidation = false; lnkNextTen.Text = "..+10"; lnkNextTen.CommandName = "Page"; lnkNextTen.CommandArgument = intNextTenPages.ToString (); ; //variant lnkNextTen.Enabled = (this.PageIndex < (this.PageCount - 1)); lnkNextTen.Enabled = ( this.PageIndex < ( this.PageCount - 1 ) ); pageControls.Controls.Add ( lnkNextTen ); pageControls.Controls.Add ( new LiteralControl ( " " ) ); } #endregion AddTheNextPageLink #region AddTheLastLink LinkButton lnkLast = new LinkButton (); lnkLast.ID = "lnkLast"; lnkLast.ToolTip = "Click here to open the Last page"; lnkLast.CausesValidation = false; lnkLast.Text = "Last Page"; lnkLast.CommandName = "Page"; lnkLast.CommandArgument = "Last"; lnkLast.Enabled = ( this.PageIndex < ( this.PageCount - 1 ) ); pageControls.Controls.Add ( lnkLast ); pageControls.Controls.Add ( new LiteralControl ( " " ) ); #endregion AddTheLastLink cell.Controls.Add ( pageControls ); // Add the cell to pager row row.Cells.Add ( cell ); #region CopyPaste //pageControls.Controls.Add ( lnkFirst ); //pageControls.Controls.Add ( new LiteralControl ( " " ) ); //LinkButton lnkPrevious = new LinkButton (); //lnkPrevious.ID = "lnkPrevious"; //lnkPrevious.ToolTip = "Previous page"; //lnkPrevious.CausesValidation = false; //lnkPrevious.Text = "<<"; //lnkPrevious.CommandName = "Page"; //lnkPrevious.CommandArgument = "Prev"; //lnkPrevious.Enabled = lnkFirst.Enabled; //pageControls.Controls.Add ( lnkPrevious ); //pageControls.Controls.Add ( new LiteralControl ( " " ) ); //LinkButton lnkNext = new LinkButton (); //lnkNext.ID = "lnkNext"; //lnkNext.ToolTip = "Next page"; //lnkNext.CausesValidation = false; //lnkNext.Text = ">>"; //lnkNext.CommandName = "Page"; //lnkNext.CommandArgument = "Next"; ////variant lnkNext.Enabled = (this.PageIndex < (this.PageCount - 1)); //lnkNext.Enabled = ( this.PageIndex < ( this.PageCount - 1 ) ); //pageControls.Controls.Add ( lnkNext ); //pageControls.Controls.Add ( new LiteralControl ( " " ) ); //LinkButton lnkLast = new LinkButton (); //lnkLast.ID = "lnkLast"; //lnkLast.ToolTip = "Last page"; //lnkLast.CausesValidation = false; //lnkLast.Text = "last"; //lnkLast.CommandName = "Page"; //lnkLast.CommandArgument = "Last"; //lnkLast.Enabled = lnkNext.Enabled; //pageControls.Controls.Add ( lnkLast ); #endregion CopyPaste // Add pager controls to cell1 #endregion GenericNumberPager //variant this.CreateBottomSummary ( ); } protected override void OnInit ( EventArgs e ) { base.OnInit ( e ); //Utils.Debugger.WriteIf ( "XGrid.cs OnInit START" ); //this.GvPageSize = 20; this.PageIndex = this.MyPageIndex; } //eof method protected override void LoadControlState ( object savedState ) { base.LoadControlState ( savedState ); } private void CreateBottomSummary () { //Utils.Debugger.WriteIf ( "XGrid.cs CreateBottomSummary START" ); if (this.ShowResultSummary && this.PageCount != 0) { //Utils.Debugger.WriteIf ( " after if " ); // Create summary controls //variant org //variant int firstResultIndex = this.PageIndex * this.PageSize; int firstResultIndex = this.PageIndex * this.PageSize; //DEBUG//Utils.Debugger.WriteIf ( "From CreateBottomSummary" ); //variant gets null each 2 time int firstResultIndex = this.PageIndex * this.GvPageSize; HtmlGenericControl topSummaryControl = new HtmlGenericControl ( "div" ); topSummaryControl.Style.Add ( "float", "left" ); topSummaryControl.InnerHtml = string.Format ( "Showing records <strong> from </strong>" + "{0} <strong> to </strong> {1} out of {2} ", firstResultIndex + 1, firstResultIndex + this.Rows.Count, this.DataSourceCount ); //add + " with " HtmlGenericControl bottomSummaryControl = new HtmlGenericControl ( "div" ); bottomSummaryControl.Style.Add ( "float", "left" ); bottomSummaryControl.InnerHtml = topSummaryControl.InnerHtml; #region AddHererTextBox ////ADD HERE TEXTBOX START ////====================================Add here the paging textbox START //TextBox txtgv = new TextBox ( ); //txtgv.ID= "global." + this.ID.ToString ( ) + "txtgv"; //txtgv.Visible = true; //txtgv.Width = new Unit ( 20, UnitType.Pixel ); //txtgv.Font.Size = new FontUnit ( FontSize.XSmall ); //Utils.Debugger.WriteIf ( "From CreateBottomSummary2" ); //txtgv.Text = System.Convert.ToString ( this.GvPageSize ); //txtgv.EnableViewState = true; ////ASSIGN EVENT HANDLERS DURING CREATE CHILD CONTROLS ////txtgv.TextChanged += new EventHandler ( this.ClickSetPageSize ); //Button butPageSetter = new Button ( ); //butPageSetter.ID = "butPageSetter"; //butPageSetter.Text = " per page "; //butPageSetter.Click += new EventHandler ( this.ClickSetPageSize ); ////txtgv.TextChanged += new EventHandler ( txtgv_TextChanged ); //bottomSummaryControl.Controls.Add ( txtgv ); //bottomSummaryControl.Controls.Add ( butPageSetter ); ////====================================Add here the paging textbox END #endregion //AddHererTextBox if (this.PageCount == 1) { // Add summary to table at the top this.Controls[0].Controls.AddAt ( 0, this.CreateSummaryRow ( topSummaryControl ) ); // Add summary to table at the bottom this.Controls[0].Controls.Add ( this.CreateSummaryRow ( bottomSummaryControl ) ); //variant bottom this.Controls.Add ( this.CreateSummaryRow ( bottomSummaryControl ) ); } //eof if else { // Add summary control to top pager if (this.TopPagerRow != null) this.TopPagerRow.Cells[0].Controls.Add ( topSummaryControl ); // Add summary control to bottom pager if (this.BottomPagerRow != null) this.BottomPagerRow.Cells[0].Controls.Add ( bottomSummaryControl ); } //eof else } //eof if ( this.ShowResultSummary && this.PageCount != 0 ) //variant base.PageSize = this.GvPageSize; } /// <summary> /// Creates the control's child controls. /// </summary> protected override int CreateChildControls ( IEnumerable dataSource, bool dataBinding ) { int controlsCreated = base.CreateChildControls ( dataSource, dataBinding ); if (this.DisplayInsertRow) { ICollection cols = this.CreateColumns ( null, false ); DataControlField[] fields = new DataControlField[cols.Count]; cols.CopyTo ( fields, 0 ); if (this.Controls.Count == 0) { // Create dummy table for inserting the first entry Table tableControl = new Table (); if (this.ShowHeader) { // Create header this._myHeaderRow = this.CreateRow ( -1, -1, DataControlRowType.Header, DataControlRowState.Normal ); this.InitializeRow ( this._myHeaderRow, fields ); // Trigger events GridViewRowEventArgs headerRowArgs = new GridViewRowEventArgs ( this._myHeaderRow ); this.OnRowCreated ( headerRowArgs ); tableControl.Rows.Add ( this._myHeaderRow ); if (dataBinding) this.OnRowDataBound ( headerRowArgs ); } // Add insert row this.Controls.Add ( tableControl ); } else // Use generated header row this._myHeaderRow = null; // Create insertion row this._insertRow = this.CreateRow ( -1, -1, DataControlRowType.DataRow, this.InsertRowActive ? DataControlRowState.Insert : DataControlRowState.Normal ); this._insertRow.ControlStyle.MergeWith ( this.AlternatingRowStyle ); this.InitializeRow ( this._insertRow, fields ); // Trigger events GridViewRowEventArgs insertRowArgs = new GridViewRowEventArgs ( this._insertRow ); this.OnRowCreated ( insertRowArgs ); // Add row to top of table, just below header this.Controls[0].Controls.AddAt ( this.Controls[0].Controls.IndexOf ( this.HeaderRow ) + 1, this._insertRow ); if (dataBinding) this.OnRowDataBound ( insertRowArgs ); } this.CreateBottomSummary (); return controlsCreated; } //eof CreateChildControls /// <summary> /// Initialises a row in the grid. /// </summary> /// <param name="row">The row to initialise.</param> /// <param name="fields">The fields with which to initialise the row.</param> protected override void InitializeRow ( GridViewRow row, DataControlField[] fields ) { base.InitializeRow ( row, fields ); if (row.RowType == DataControlRowType.Header && this.AscendingImageUrl != null) { for (int i = 0; i < fields.Length; i++) { if (this.SortExpression.Length > 0 && fields[i].SortExpression == this.SortExpression) { // Add sort indicator Image sortIndicator = new Image (); sortIndicator.ImageUrl = this.SortDirection == SortDirection.Ascending ? this.AscendingImageUrl : this.DescendingImageUrl; sortIndicator.Style.Add ( HtmlTextWriterStyle.VerticalAlign, "TextTop" ); row.Cells[i].Controls.Add ( sortIndicator ); break; } } } foreach (TableCell cell in row.Cells) { // If we have an insert button it should only be shown on the insert row foreach (Control control in cell.Controls) { IButtonControl button = control as IButtonControl; if (row == this._insertRow) { if (row.RowState == DataControlRowState.Normal) { // Hide all controls other than the insert button on the insert row control.Visible = false; if (button != null && ( button.CommandName == "Insert" || button.CommandName == "New" )) control.Visible = true; } else if (button != null && button.CommandName == "Cancel") // Hide cancel button on insert row control.Visible = false; } else if (button != null && button.CommandName == "New") // Hide the insert button from all other rows control.Visible = false; } } } private IOrderedDictionary _insertValues; private bool HandleInsertCallback ( int affectedRows, Exception ex ) { GridViewInsertedEventArgs e = new GridViewInsertedEventArgs ( this._insertValues, ex ); this.OnRowInserted ( e ); if (ex != null && !e.ExceptionHandled) return false; this.RequiresDataBinding = true; return true; } /// <summary> /// Raises the <see cref="GridView.RowCommand"/> event. /// </summary> /// <param name="e">Event data.</param> protected override void OnRowCommand ( GridViewCommandEventArgs e ) { base.OnRowCommand ( e ); if (e.CommandName == "New") { this.InsertRowActive = true; this.EditIndex = -1; this.RequiresDataBinding = true; } else if (e.CommandName == "Edit") this.InsertRowActive = false; else if (e.CommandName == "Insert") { // Perform validation if necessary bool doInsert = true; IButtonControl button = e.CommandSource as IButtonControl; if (button != null) { if (button.CausesValidation) { this.Page.Validate ( button.ValidationGroup ); doInsert = this.Page.IsValid; } } if (doInsert) { // Get values this._insertValues = new OrderedDictionary (); this.ExtractRowValues ( this._insertValues, this._insertRow, true, false ); GridViewInsertEventArgs insertArgs = new GridViewInsertEventArgs ( this._insertRow, this._insertValues ); this.OnRowInserting ( insertArgs ); if (!insertArgs.Cancel && this.IsBoundUsingDataSourceID) { // Get data source DataSourceView data = this.GetData (); data.Insert ( this._insertValues, this.HandleInsertCallback ); } } } } //eof method OnRowCommand protected override void OnRowUpdating ( GridViewUpdateEventArgs e ) { base.OnRowUpdating ( e ); } protected override void OnPageIndexChanging ( GridViewPageEventArgs e ) { this.DataSource = this.SortDataTable ( this.DataSource as DataTable, true ); //this.PageIndex = e.NewPageIndex; this.MyPageIndex = e.NewPageIndex; //Utils.Debugger.WriteIf ( "From OnPageIndexChanging" ); this.DataBind (); //databind again after sorting } //eof method protected override void DataBind ( bool raiseOnDataBinding ) { //this.PageSize = this.GetPagerSize ( ); //this.PageSize = this.GvPageSize; base.DataBind ( raiseOnDataBinding ); this.PageIndex = this.MyPageIndex; } /// <summary> /// Renders the contents of the control. /// </summary> /// <param name="writer">The <see cref="HtmlTextWriter"/> to write to.</param> protected override void RenderContents ( HtmlTextWriter writer ) { if (String.IsNullOrEmpty ( this.StrLabMsgText ) == false) { writer.WriteBeginTag ( "div" ); writer.WriteBeginTag ( "p" ); writer.Write ( this.StrLabMsgText ); writer.WriteEndTag ( "p" ); writer.WriteEndTag ( "div" ); } base.RenderContents ( writer ); } //eof method RenderContents //OLD private TableRow CreateSummaryRow ( Control summaryControl ) //{ // TableRow summaryRow = new TableRow ( ); // TableCell summaryCell = new TableCell ( ); // summaryCell.ColumnSpan = this.HeaderRow.Cells.Count; // summaryRow.Cells.Add ( summaryCell ); // summaryCell.Controls.Add ( summaryControl ); // return summaryRow; //} //eof method CreateSummaryRow private GridViewRow CreateSummaryRow ( Control summaryControl ) { GridViewRow summaryRow = new GridViewRow ( 0, 0, DataControlRowType.EmptyDataRow, DataControlRowState.Normal ); TableCell summaryCell = new TableCell (); summaryCell.ColumnSpan = this.HeaderRow.Cells.Count; summaryRow.Cells.Add ( summaryCell ); summaryCell.Controls.Add ( summaryControl ); return summaryRow; } //eof method CreateSummaryRow protected override void OnRowCreated ( GridViewRowEventArgs e ) { //Utils.Debugger.WriteIf ( "START --- XGrid -- OnRowCreated" ); base.OnRowCreated ( e ); #region HideColumnsSetToBeHidden System.Collections.Specialized.ListDictionary ld = this.Ld; if (ld != null) { for (int i = 0; i < e.Row.Cells.Count; i++) { string key; int intColNumberToHide; if (i < 9) key = "0" + i.ToString () + "HideColumn"; else key = i.ToString () + "HideColumn"; if (key != null && ld != null && ld[key] != null && String.IsNullOrEmpty ( ld[key].ToString () ) == false) { #region TryToGetANumber try { intColNumberToHide = System.Convert.ToInt16 ( ld[key] ); if (intColNumberToHide == 1) { e.Row.Cells[i].Visible = false; } else { //do nothing by default all this.columns are visible !!! } } #endregion TryToGetANumber #region CatchExceptions catch (Exception ex) { } #endregion CatchExceptions } //eof if (String.IsNullOrEmpty ( ld[key].ToString() ) == false) } } //eof if (ld != null) #endregion HideColumnsSetToBeHidden if (e.Row.RowType == DataControlRowType.DataRow) { if (( e.Row.RowIndex % 2 ) == 0) e.Row.BackColor = System.Drawing.ColorTranslator.FromHtml ( "#EFEFEF" ); } //eof if //Utils.Debugger.WriteIf ( "END --- XGrid -- OnRowCreated" ); } //eof method protected override void OnDataBound ( EventArgs e ) { base.OnDataBound ( e ); } protected override void OnRowDataBound ( GridViewRowEventArgs e ) { //Utils.Debugger.WriteIf ( "START --- XGrid --- OnRowDataBound" ); base.OnRowDataBound ( e ); if (e.Row.RowType == DataControlRowType.Header) { } } //eof method private bool DisplayInsertRow { get { return this.AllowInserting && this.PageIndex == 0; } } #endregion #region TemplateMethods protected override object SaveViewState () { return new Pair ( base.SaveViewState (), null ); } protected override void LoadViewState ( object savedState ) { base.LoadViewState ( ( (Pair)savedState ).First ); EnsureChildControls (); } protected override void CreateChildControls () { base.CreateChildControls (); } #endregion //TemplateMethods #region UtilityMethods /// <summary> /// Search for a control within the passed root control by the control id passed as string /// </summary> /// <param name="root">the upper control to start to search for</param> /// <param name="id">the id of the control as string</param> /// <returns></returns> private Control FindControlRecursively ( Control root, string id ) { try { if (root.ID == id) { return root; } //eof if foreach (Control c in root.Controls) { Control t = this.FindControlRecursively ( c, id ); if (t != null) { return t; } //eof if } //eof foreach } //eof try catch (Exception e) { //Utils.Debugger.WriteIf ( "The following Exception occured : \n" + e.Message ); return null; } //eof catch return null; } //eof FindControlRecursive protected DataView SortDataTable ( DataTable dataTable, bool isPageIndexChanging ) { //Utils.Debugger.WriteIf ("SortDataTable(DataTable dataTable, bool isPageIndexChanging) START"); if (dataTable != null) { DataView dataView = new DataView ( dataTable ); if (GridViewSortExpression != string.Empty) { try { if (isPageIndexChanging) { dataView.Sort = string.Format ( "{0} {1}", GridViewSortExpression, GridViewSortDirection ); } else { dataView.Sort = string.Format ( "{0} {1}", GridViewSortExpression, GetSortDirection () ); } } catch (Exception e) { } } return dataView; } else { return new DataView (); } } //eof method SortDataTable #endregion //UtilityMethods } //eof class /// <summary> /// Provides data for the <see cref="ExtendedGridView.RowInserting"/> event. /// </summary> public class GridViewInsertEventArgs : CancelEventArgs { #region Constructor /// <summary> /// Initialises a <see cref="GridViewInsertEventArgs"/>. /// </summary> /// <param name="row">The insertion row.</param> /// <param name="newValues">The values for the new row.</param> public GridViewInsertEventArgs ( GridViewRow row, IOrderedDictionary newValues ) { this._row = row; this._values = newValues; } #endregion #region Properties #region Row private GridViewRow _row; /// <summary> /// The row containing the input controls. /// </summary> public GridViewRow Row { get { return this._row; } } #endregion #region NewValues private IOrderedDictionary _values; /// <summary> /// The values for the new row to insert. /// </summary> public IOrderedDictionary NewValues { get { return this._values; } } #endregion #endregion } /// <summary> /// Provides data for the <see cref="ExtendedGridView.RowInserted"/> delegate. /// </summary> public class GridViewInsertedEventArgs : EventArgs { #region Constructor /// <summary> /// Initialises a <see cref="GridViewInsertedEventArgs"/>. /// </summary> /// <param name="newValues">The values for the new row.</param> /// <param name="ex">The exception that occurred during the insert, if any.</param> public GridViewInsertedEventArgs ( IOrderedDictionary newValues, Exception ex ) { this._values = newValues; this._exception = ex; } #endregion #region Properties #region NewValues private IOrderedDictionary _values; /// <summary> /// The values that were inserted into the new row. /// </summary> public IOrderedDictionary NewValues { get { return this._values; } } #endregion #region Exception private Exception _exception; /// <summary> /// The exception that occurred during the insert, if any. /// </summary> public Exception Exception { get { return this._exception; } } #endregion #region ExceptionHandled private bool _exceptionHandled; /// <summary> /// Whether the exception that occurred during the insert has been handled. /// </summary> public bool ExceptionHandled { get { return this._exceptionHandled; } set { this._exceptionHandled = value; } } #endregion #endregion } //eof class GridViewInsertedEventArgs } //eof namespace Controls
Getting the Current TabItem when the Tab is not selected in WPF
-
[image: Banner]
This is a quick reminder to self on how to retrieve a TabItem from a WPF
TabControl *when the tab is not currently selected* because I ru...
1 week ago
No comments:
Post a Comment
- the first minus - Comments have to be moderated because of the spammers
- the second minus - I am very lazy at moderating comments ... hardly find time ...
- the third minus - Short links are no good for security ...
- The REAL PLUS : Any critic and positive feedback is better than none, so your comments will be published sooner or later !!!!