Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I use the GridViewControl. Here it is the code:
C#
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZzimaAuctionBLL;
using ZzimaAuctionCommon;
using System.Data;

namespace ZzimaAuctionUI.Controls.UserControls
{
    public partial class GridViewControl : System.Web.UI.UserControl
    {
        #region Fields

        #endregion

        #region Properties

        public event EventHandler CheckBoxCheckedChanged;

        protected virtual void OnCheckBoxCheckedChanged()
        {
            if (CheckBoxCheckedChanged != null)
            {
                CheckBoxCheckedChanged(this, EventArgs.Empty);
            }
        }

       

        public bool EnableCheckBoxColumn
        {
            get
            {
                return gvMain.Columns[0].Visible;
            }
            set
            {
                gvMain.Columns[0].Visible = value;
            }
        }

        public Type ListClassType
        {
            get { return (Type)ViewState["ListClassName"]; }
            set { ViewState["ListClassName"] = value; }
        }

        public string LoadMethodName
        {
            get { return ViewState["LoadMethodName"].ToString(); }
            set { ViewState["LoadMethodName"] = value; }
        }

        public string GetRowsCountMethodName
        {
            get { return ViewState["GetRowsCountMethodName"].ToString(); }
            set { ViewState["GetRowsCountMethodName"] = value; }
        }

        public int IDColumnIndex
        {
            get
            {
                if (ViewState["IDColumnIndex"] == null)
                {
                    return -1;
                }
                return (int)ViewState["IDColumnIndex"];
            }
            set { ViewState["IDColumnIndex"] = value; }
        }

        public List<int> SelectedRowIDs
        {
            get
            {
                List<int> result = new List<int>();

                var rows = gvMain.Rows;

                for (int i = 0; i < rows.Count; ++i)
                {
                    bool isChecked = ((from Control control in rows[i].Cells[0].Controls
                                       where control.ID == "chbxSelect"
                                       select control).Single() as CheckBox).Checked;

                    if (isChecked)
                    {
                        result.Add((int)gvMain.DataKeys[i].Value);
                    }
                }

                return result;
            }
        }

        public int PageIndex
        {
            get
            {
                if (ViewState["PageIndex"] == null)
                {
                    return 0;
                }
                return (int)ViewState["PageIndex"];
            }
            protected set
            {
                if (value >= 0)
                {
                    ViewState["PageIndex"] = value;
                }
            }
        }

        public int PageSize
        {
            get
            {
                return gvMain.PageSize;
            }
            set
            {
                gvMain.PageSize = value;
            }
        }

        public int PagesCount
        {
            get
            {
                if (ViewState["PagesCount"] == null)
                {
                    return 0;
                }
                return (int)ViewState["PagesCount"];
            }
            protected set
            {
                if (value >= 0)
                {
                    ViewState["PagesCount"] = value;
                }
            }
        }

        public int RecordsCount
        {
            get
            {
                if (ViewState["RecordsCount"] == null)
                {
                    return 0;
                }
                return (int)ViewState["RecordsCount"];
            }
            protected set
            {
                if (value >= 0)
                {
                    ViewState["RecordsCount"] = value;
                }
            }
        }

        public string CurrentSortExpression
        {
            get
            {
                if (ViewState["CurrentSortExpression"] == null)
                {
                    return string.Empty;
                }
                return (string)ViewState["CurrentSortExpression"];
            }
            set
            {
                ViewState["CurrentSortExpression"] = value;
            }
        }

        public SortDirection CurrentSortDirection
        {
            get
            {
                if (ViewState["CurrentSortDirection"] == null)
                {
                    return SortDirection.Ascending;
                }
                return (SortDirection)ViewState["CurrentSortDirection"];
            }
            set
            {
                ViewState["CurrentSortDirection"] = value;
            }
        }

        public HorizontalAlign HorizontalAlign
        {
            get
            {
                return gvMain.HorizontalAlign;
            }
            set
            {
                gvMain.HorizontalAlign = value;
            }
        }

        public Unit Width
        {
            get
            {
                return Unit.Parse(tblMain.Style[HtmlTextWriterStyle.Width]);
            }
            set
            {
                tblMain.Style[HtmlTextWriterStyle.Width] = value.ToString();
            }
        }

        #endregion

        #region Methods

        public override void DataBind()
        {
            BindGridView(PageIndex, CurrentSortExpression, CurrentSortDirection);
            UpdatePagerInfo();
            base.DataBind();
        }

        private void BindGridView(int startRowIndex, string sortExpression, SortDirection sortDirection)
        {
            object listObject = Activator.CreateInstance(ListClassType);

            gvMain.DataSource = ListClassType.InvokeMember(
                LoadMethodName, BindingFlags.InvokeMethod, null, listObject,
                new object[] { 
                    new DataOutputBO { 
                        StartIndex = startRowIndex * PageSize,
                        PageSize = gvMain.PageSize,
                        SortExpression = CurrentSortExpression,
                        SortDirection = CurrentSortDirection}});

            GetTotalRowCount(listObject);

            gvMain.DataBind();

            CurrentSortExpression = sortExpression;
            CurrentSortDirection = sortDirection;

            listObject = null;
        }

        private void TuneNavigationButtons()
        {
            lnkFirstPage.Enabled = lnkPrevPage.Enabled = (PageIndex > 0);
            lnkLastPage.Enabled = lnkNextPage.Enabled = (PageIndex != PagesCount - 1);
        }

        private void UpdateSortIndicator(GridViewRow headerRow)
        {
            for (int i = 0; i < headerRow.Cells.Count; ++i)
            {

                if (gvMain.Columns[i].SortExpression == CurrentSortExpression && CurrentSortExpression != string.Empty)
                {
                    Image sortIndicator = new Image();

                    sortIndicator.ImageUrl =
                        (CurrentSortDirection == SortDirection.Ascending)
                        ? ("~/Design/Images/icon-sort1.gif")   //( AscendingImageUrl  )
                        : ("~/Design/Images/icon-sort2.gif"); //( DescendingImageUrl );

                    sortIndicator.Style.Add("style", "vertical-align:middle;float:right");
                    //sortIndicator.ImageAlign = ImageAlign.Right;
                    headerRow.Cells[i].Controls.Add(sortIndicator);
                }
            }
        }

        private void UpdatePagerInfo()
        {
            lblRecordsNum.Text = RecordsCount.ToString();
            lblPageInfo.Text = (PageIndex + 1).ToString();
            lblMaxPages.Text = PagesCount.ToString();
        }

        protected void GetTotalRowCount(object listObject)
        {
            RecordsCount = (int)ListClassType.InvokeMember(
                GetRowsCountMethodName, BindingFlags.InvokeMethod, null, listObject,
                new object[] { });

            PagesCount = RecordsCount / gvMain.PageSize + (RecordsCount % gvMain.PageSize > 0 ? 1 : 0);
        }

        protected void GetTotalRowCount()
        {
            object listObject = Activator.CreateInstance(ListClassType);

            GetTotalRowCount(listObject);

            listObject = null;
        }

        public void AddBoundIDField(string dataField, string sortExpression)
        {
            BoundField boundField = new BoundField();

            if (dataField != string.Empty)
            {
                boundField.DataField = dataField;
            }
            if (sortExpression != string.Empty)
            {
                boundField.SortExpression = sortExpression;
            }

            boundField.Visible = false; // Скрываем столбец с ID

            gvMain.Columns.Add(boundField);
            gvMain.DataKeyNames = new string[] { dataField }; // В коллекцию DataKeys собираем ID

            IDColumnIndex = gvMain.Columns.IndexOf(boundField);
        }

        public void AddBoundField(string dataField, string headerText, string sortExpression, Unit columnWidth)
        {
            BoundField boundField = new BoundField();

            if (dataField != string.Empty)
            {
                boundField.DataField = dataField;
            }

            if (headerText != string.Empty)
            {
                boundField.HeaderText = headerText;
            }

            if (sortExpression != string.Empty)
            {
                boundField.SortExpression = sortExpression;
            }

            boundField.HeaderStyle.Width = columnWidth;

            gvMain.Columns.Add(boundField);
        }

        public void AddBoundField(string dataField, string headerText, string sortExpression)
        {
            AddBoundField(dataField, headerText, sortExpression, Unit.Empty);
        }

        #endregion

        #region Event Handlers

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
            {
                CheckBoxCheckedChanged = (EventHandler)Session["CheckBoxCheckedChanged"];
            }
            else
            {
                TuneNavigationButtons();

            }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            Session["CheckBoxCheckedChanged"] = CheckBoxCheckedChanged;
        }

        protected void chbxSelect_OnCheckedChanged(object sender, EventArgs e)
        {
            OnCheckBoxCheckedChanged();
        }

        protected void Navigate(object sender, EventArgs e)
        {
            var button = (sender as IButtonControl);

            switch (button.CommandArgument)
            {
                case "First":
                    PageIndex = 0;
                    break;
                case "Last":
                    PageIndex = PagesCount - 1;
                    break;
                case "Next":
                    if (PageIndex < PagesCount - 1)
                    {
                        PageIndex++;
                    }
                    break;
                case "Prev":
                    if (PageIndex > 0)
                    {
                        PageIndex--;
                    }
                    break;
            }
            TuneNavigationButtons();
            DataBind();
        }

        protected void gvMain_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (CurrentSortExpression == e.SortExpression)
            {
                if (CurrentSortDirection == SortDirection.Ascending)
                {
                    CurrentSortDirection = SortDirection.Descending;
                }
                else
                {
                    CurrentSortDirection = SortDirection.Ascending;
                }
            }
            else
            {
                CurrentSortExpression = e.SortExpression;
                CurrentSortDirection = SortDirection.Ascending;
            }

            DataBind();
        }

        protected void gvMain_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                UpdateSortIndicator(e.Row);
            }
        }


        #endregion
    }
}


In method to bind gridview I can pass just the name of listclass.
C#
private void BindGridView(int startRowIndex, string sortExpression, SortDirection sortDirection)
        {
            object listObject = Activator.CreateInstance(ListClassType);
            gvMain.DataSource = ListClassType.InvokeMember(
                LoadMethodName, BindingFlags.InvokeMethod, null, listObject,
                new object[] {
                    new DataOutputBO {
                        StartIndex = startRowIndex * PageSize,
                        PageSize = gvMain.PageSize,
                        SortExpression = CurrentSortExpression,
                        SortDirection = CurrentSortDirection}});
            GetTotalRowCount(listObject);
            gvMain.DataBind();
            CurrentSortExpression = sortExpression;
            CurrentSortDirection = sortDirection;
            listObject = null;
        }


How to rewrite or extend this method for I can bind my control with some object of listclass. Now there is a class f.e. TagBOList wich has a method public List<tagbo> GetData().

But now I need to use GetData methods with some parameters or bind my control with concrete object of listclass. Which solution is more effective and How to solve this problem?
Posted
Updated 31-Jul-10 10:30am
v2
Comments
raju melveetilpurayil 31-Jul-10 12:50pm    
do post small block of code/discription where you get problem.
koool.kabeer 31-Jul-10 14:11pm    
hard to read

1 solution

No-one is going to read all this code. If you bind to a list, then you can just write a template to pull the data you want from each object. Your ASP.NET book should have good examples of this.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900