Grid View wrapper (asp.net)

    /// <Summary>
    /// grid view wrapper.
    /// </ Summary>
    Public Class GridViewWrapper
    {
        #Region constant defined
        /// <summary>
        key name of ViewState to store /// list information
        /// </ Summary>
        Private Const String GRIDVIEW_DATA = "GRIDVIEW_DATA";

        /// <summary>
        key name of ViewState to store the column name /// last sort was carried out
        /// </ Summary>
        Private Const String GRID_SORT_COLUMN = "GRID_SORT_COLUMN";

        /// <summary>
        key name of ViewState to store the sort direction that was carried out /// most recent sort
        /// </ Summary>
        Private Const String GRID_SORT_DIRECTION = "GRID_SORT_DIRECTION";
        #Endregion

        #region variable definition
        /// <Summary>
        /// list of search results grid view
        /// </ Summary>
        Protected GridView Gv;

        /// <summary>
        /// ViewState
        /// </summary>
        private StateBag viewState;

        private string checkedStatusColumn;
        #endregion

        #region event definition
        /// <Summary>
        /// events before the grid to draw
        /// </ Summary>
        /// <Param Name = "Sender"> </ Param>
        /// <Param Name = " E "> </ Param>
        Protected Void Gv_PreRender (Object Sender, EventArgs E)
        {
            If (((GridView) Sender) .Controls.Count> 0
                Andoando ((GridView) Sender) .Controls [0] .Controls.Count> 0 )
                // Pager line always displays
                ((GridView) sender) .Controls [0] .Controls [0] .Visible = true;
        }

        /// <summary>
        event when you click the link in the header portion of /// grid
        /// </ Summary>
        /// <Param Name = "Sender"> </ Param>
        /// <Param Name = "e"> </ param>
        protected void Gv_Sorting (object sender, GridViewSortEventArgs e)
        {
            obtaining search results from ViewState //
            IList <IBaseDTO> result = (IList <IBaseDTO>) getViewState (GRIDVIEW_DATA);

            // If the search results can be acquired
            If (Result! = Null)
            {
                If (E.SortExpression.Equals ((String) GetViewState (GRID_SORT_COLUMN)))
                {
                    // If the last sort column matches the current sort column

                    // If the last sort direction is ascending
                    If (ListUtil.SortDirection.Asc.Equals ((ListUtil.SortDirection) GetViewState (GRID_SORT_DIRECTION)))
                        // store the current sort direction or descending order in ViewState
                        SetViewState (GRID_SORT_DIRECTION, ListUtil. SortDirection.Desc);
                    the else
                        // store the current sort direction-Ascending ViewState
                        setViewState (GRID_SORT_DIRECTION, ListUtil.SortDirection.Asc);
                }
                the else
                {
                    // the previous sort column, when the current sort column different

                    // store the current sort direction-Ascending ViewState
                    setViewState (GRID_SORT_DIRECTION, ListUtil.SortDirection.Asc);
                }

                // store the current sort column ViewState
                setViewState (GRID_SORT_COLUMN, E.SortExpression);

                // out the sort the search results
                ListUtil.Sort (result, e.SortExpression, (ListUtil.SortDirection ) GetViewState (GRID_SORT_DIRECTION));

                // store the sort results to the Session
                setViewState (GRIDVIEW_DATA, result);

                // bind the search results to the grid
                DataBind ();
            }
        }

        /// <Summary>
        /// search event at the time of data-bound results for the grid
        /// </ Summary>
        /// <Param Name = "Sender"> </ Param>
        /// <Param Name = " e "> </ param>
        protected void Gv_RowDataBound (object sender, GridViewRowEventArgs e)
        {
            an if (DataControlRowType.DataRow.Equals (E.Row.RowType))
            {
                // if the DataRow
            }
            the else an if (DataControlRowType.Footer.Equals (e .Row.RowType))
            {
                // If the Footer

                // 合計金額を合算し、設定する
                IList<IBaseDTO> list = (IList<IBaseDTO>)GetViewState(GRIDVIEW_DATA);
                for (int i = 0; i < e.Row.Cells.Count; i++)
                {
                    if (((GridView)sender).Columns[i] is ClmsBoundField)
                    {
                        if (((ClmsBoundField)((GridView)sender).Columns[i]).IsTotalColumn)
                        {
                            e.Row.Cells[i].Text = StringUtil.FormatNumberAddComma(ListUtil.Total(list, ((ClmsBoundField)((GridView)sender).Columns[i]).DataField));
                        }
                    }
                }
            }
            else if (DataControlRowType.Pager.Equals(e.Row.RowType))
            {
                // Pagerの場合

                // search to calculate the number of results
                int resultCnt = ((IList <IBaseDTO >) GetViewState (GRIDVIEW_DATA)) Count.;

                // Set the number of pages that are displayed now
                ((Label) E.Row.FindControl ( "LblCurrentPage")) Text = (((GridView) Sender) .PageIndex Tasu 1) .ToString ();.
                // total number of pages setting
                . ((Label) E.Row.FindControl ( "LblPages")) Text = ((GridView) sender) .PageCount.ToString ();
                // set the number of results
                ((Label) e.Row . .FindControl ( "lblCnt")) Text = resultCnt.ToString ();

                // ページリンクボタンの制御
                ((LinkButton)e.Row.FindControl("lkbFirstPage")).Enabled = false;
                ((LinkButton)e.Row.FindControl("lkbPrevPage")).Enabled = false;
                ((LinkButton)e.Row.FindControl("lkbNextPage")).Enabled = false;
                ((LinkButton)e.Row.FindControl("lkbLastPage")).Enabled = false;
                if (((GridView)sender).PageIndex == 0 && ((GridView)sender).PageCount > 1)
                {
                    // Firstページの場合
                    ((LinkButton)e.Row.FindControl("lkbNextPage")).Enabled = true;
                    ((LinkButton)e.Row.FindControl("lkbLastPage")).Enabled = true;
                }
                else if (((GridView)sender).PageIndex == ((GridView)sender).PageCount - 1
                    && ((GridView)sender).PageCount > 1)
                {
                    // Lastページの場合
                    ((LinkButton)e.Row.FindControl("lkbFirstPage")).Enabled = true;
                    ((LinkButton)e.Row.FindControl("lkbPrevPage")).Enabled = true;
                }
                else if (((GridView)sender).PageCount > 1)
                {
                    // 上記以外の場合
                    ((LinkButton)e.Row.FindControl("lkbNextPage")).Enabled = true;
                    ((LinkButton)e.Row.FindControl("lkbLastPage")).Enabled = true;
                    ((LinkButton)e.Row.FindControl("lkbFirstPage")).Enabled = true;
                    ((LinkButton)e.Row.FindControl("lkbPrevPage")).Enabled = true;
                }
            }
        }

        /// <Summary>
        /// RowCommand of events
        /// </ Summary>
        /// <Param Name = "Sender"> </ Param>
        /// <Param Name = "E"> </ Param>
        Protected Gv_RowCommand void (object sender, GridViewCommandEventArgs e)
        {
            // save the state of the check box
            SaveCheckBoxStatus ();

            if (e.CommandName == "NextPage")
            {
                ((GridView)sender).PageIndex++;

                // results bind to the grid
                DataBind ();
            }
            the else an if (E.CommandName == "LastPage")
            {
                ((GridView) sender) .PageIndex = ((GridView) sender) .PageCount - 1;

                // results bind to the grid
                DataBind ();
            }
            the else an if (E.CommandName == "FirstPage")
            {
                ((GridView) sender) .PageIndex = 0;

                // bind the search results to the grid
                DataBind ();
            }
            the else an if (E.CommandName == "PrevPage")
            {
                ((GridView) sender) .PageIndex--;

                // bind the search results to the grid
                DataBind ();
            }
        }
        #endregion

        #region internal function
        /// <Summary>
        /// to get the value from ViewState from the key
        /// </ Summary>
        /// <Param Name = "Key"> </ Param>
        /// <Returns> < / returns>
        private object getViewState (string key)
        {
            return This.ViewState [This.Gv.ID + "_" + key];
        }

        /// <summary>
        /// ViewStateに値を設定する
        /// </summary>
        /// <param name="key"></param>
        /// <param name="obj"></param>
        private void SetViewState(string key, object obj)
        {
            this.viewState[this.gv.ID + "_" + key] = obj;
        }
        #endregion

        #region properties
        /// <summary>
        /// List get and set the display data
        /// </ summary>
        public IList <IBaseDTO> DataSource
        {
            The get {return (IList <IBaseDTO>) getViewState (GRIDVIEW_DATA);}
            The set setViewState {(GRIDVIEW_DATA, value);}
        }

        /// <summary>
        /// PageIndex
        /// </summary>
        public int PageIndex
        {
            get { return this.gv.PageIndex; }
            set { this.gv.PageIndex = value; }
        }

        /// <summary>
        /// DataKeyNames
        /// </summary>
        public string[] DataKeyNames
        {
            get { return this.gv.DataKeyNames; }
        }
        #endregion

        #region external function
        /// <summary>
        to bind the /// list data to the grid.
        /// </ summary>
        public void DataBind ()
        {
            // bind the search results to the grid
            This.Gv.DataSource = (IList <IBaseDTO>) getViewState (GRIDVIEW_DATA);
            This.Gv.DataBind ();
        }

        /// <summary>
        /// to save the state of the check box grid view
        /// </ summary>
        public void SaveCheckBoxStatus ()
        {
            an if (string.IsNullOrEmpty (This.CheckedStatusColumn))
                return;

            string chkCtrlNm = "chk" + this.checkedStatusColumn;
            IList<IBaseDTO> list = (IList<IBaseDTO>)GetViewState(GRIDVIEW_DATA);
            int startIdx = this.gv.PageIndex * this.gv.PageSize;
            foreach (GridViewRow row in this.gv.Rows)
            {
                list[startIdx + row.RowIndex].GetType().GetProperty(
                    this.checkedStatusColumn).SetValue(list[startIdx + row.RowIndex],
                    ((CheckBox)row.FindControl(chkCtrlNm)).Checked,
                    null);
            }
        }
        #endregion

        #region constructor
        /// <Summary>
        /// constructor
        /// </ Summary>
        /// <Param Name = "Gv"> GridView </ Param>
        /// <Param Name = "ViewState"> ViewState </ param>
        public GridViewWrapper (GridView gv, StateBag viewState)
        {
            This.Gv = gv;
            This.ViewState = viewState;

            this.gv.Sorting += new GridViewSortEventHandler(gv_Sorting);
            this.gv.RowDataBound += new GridViewRowEventHandler(gv_RowDataBound);
            this.gv.RowCommand += new GridViewCommandEventHandler(gv_RowCommand);
            this.gv.PreRender += new EventHandler(gv_PreRender);
        }

        /// <summary>
        /// コンストラクタ
        /// </summary>
        /// <param name="gv">GridView</param>
        /// <param name="viewState">ViewState</param>
        /// <param name="checkedStatusColumn">チェックボックス名</param>
        public GridViewWrapper(GridView gv, StateBag viewState, string checkedStatusColumn)
        {
            this.gv = gv;
            this.viewState = viewState;
            this.checkedStatusColumn = checkedStatusColumn;

            this.gv.Sorting += new GridViewSortEventHandler(gv_Sorting);
            this.gv.RowDataBound += new GridViewRowEventHandler(gv_RowDataBound);
            this.gv.RowCommand += new GridViewCommandEventHandler(gv_RowCommand);
            this.gv.PreRender += new EventHandler(gv_PreRender);
        }
        #endregion
    }

Reproduced in: https: //my.oschina.net/cjkall/blog/195906

Guess you like

Origin blog.csdn.net/weixin_33675507/article/details/91756259