Solved

Sorting and paging in a dynamic gridview control.

Posted on 2011-02-16
2
739 Views
Last Modified: 2012-05-11
I am dynamically creating a series of tab, each with a gridview binding to data. Everything works great but I would like to allow sorting and paging.  I have tried several example I found online but nothing seems to work correctly.

What is the easiest way to achieve this?  When a create a Sorting event, I can't see the gridview control.  Does this event need to be part of the loop?

Any advice is appreciated.  My code to create the tabs and gridviews is below:
************************************************************************************************
protected void createMyOrgTabs()
        {
            tabCY.HeaderText = DateTime.Now.Year.ToString();

            //Create SQL Connection
            SqlConnection cn = new SqlConnection();
            cn.ConnectionString = ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString;
            cn.Open();

            //Run Insert Stored procedure
            SqlCommand cmd = new SqlCommand();
            SqlDataReader rs;
            cmd.CommandText = "myCountProcedure";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@param1", Session["param"].ToString()));
            cmd.Connection = cn;
            rs = cmd.ExecuteReader();
            rs.Read();

            int Count = Convert.ToInt32(rs["value"]);
           
            //Clear Parameters
            cmd.Parameters.Clear();
           
            //Close Datareader
            rs.Close();
           
            //Close Connection
            cn.Close();
            //Dispose Connection
            //cn.Dispose();
           
            for(int i = 1; i <= Count; i++)
            {
                //Create Dynamic TabPanels
                AjaxControlToolkit.TabPanel tp = new AjaxControlToolkit.TabPanel();
                tp.ID = tp + i.ToString();
                tp.HeaderText = DateTime.Now.AddYears(-i).Year.ToString();
                tabMyOrgs.Controls.Add(tp);

                //Open SQL Connection
                cn.Open();

                //Run Insert Stored procedure
                cmd.CommandText = "myDetailProcedure";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@param1", Session["param"].ToString()));
                cmd.Parameters.Add(new SqlParameter("@param2", DateTime.Now.AddYears(-i).Year.ToString()));
                cmd.Connection = cn;
                rs = cmd.ExecuteReader();

                DataTable dt = new DataTable();
                dt.Load(rs);
                //Session["myDT"] = dt;
                dt.DefaultView.Sort = "orgName";

                //Create Year Label
                TextBox lblYear = new TextBox();
                lblYear.Text = DateTime.Now.AddYears(-i).Year.ToString() + " Organizations";
                lblYear.CssClass = "id";

                //Create Gridview
                GridView gvMyOrgs = new GridView();
                gvMyOrgs.DataSource = dt.DefaultView;
                gvMyOrgs.ID = "gvMyOrgs";
                gvMyOrgs.AutoGenerateColumns = false;
                gvMyOrgs.AllowSorting = true;
                gvMyOrgs.Sorting += new GridViewSortEventHandler(this.gvMyOrgs_Sorting);
                gvMyOrgs.EnableSortingAndPagingCallbacks = true;
                gvMyOrgs.AlternatingRowStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#ecedf0");
                gvMyOrgs.Attributes.Add("bordercolor", "#999999");
                gvMyOrgs.CssClass = "myOrgsGView";
                gvMyOrgs.ShowHeader = true;
                gvMyOrgs.HeaderStyle.CssClass = "gview_head";
               
                //Record ID
                BoundField orgID = new BoundField();
                orgID.DataField = "id";
                orgID.HeaderText = "ID";
                orgID.SortExpression = "id";
                orgID.HeaderStyle.CssClass = "gview_center gview_head";
                orgID.ItemStyle.Width = new Unit(50, UnitType.Pixel);
                orgID.ItemStyle.CssClass = "gview_center gview_row";
               
                //Organization Name
                BoundField orgName = new BoundField();
                orgName.DataField = "orgName";
                orgName.HeaderText = "Organization";
                orgID.SortExpression = "orgName";
                orgName.HeaderStyle.CssClass = "gview_left gview_head";
                orgName.ItemStyle.Width = new Unit(300, UnitType.Pixel);
                orgName.ItemStyle.CssClass = "gview_left gview_row";

                //Location (City : State)
                BoundField cs = new BoundField();
                cs.DataField = "cs";
                cs.HeaderText = "Location";
                cs.HeaderStyle.CssClass = "gview_left gview_head";
                cs.ItemStyle.Width = new Unit(150, UnitType.Pixel);
                cs.ItemStyle.CssClass = "gview_left gview_row";
               
                //Employee's Role
                BoundField title = new BoundField();
                title.DataField = "EmpRole";
                title.HeaderText = "Employee Role";
                title.HeaderStyle.CssClass = "gview_left gview_head";
                title.ItemStyle.Width = new Unit(200, UnitType.Pixel);
                title.ItemStyle.CssClass = "gview_left gview_row";

                //Hours
                BoundField hrs = new BoundField();
                hrs.DataField = "hrsText";
                hrs.HeaderText = "Time";
                hrs.HeaderStyle.CssClass = "gview_left gview_head";
                hrs.ItemStyle.Width = new Unit(75, UnitType.Pixel);
                hrs.ItemStyle.CssClass = "gview_left gview_row";

                //Year Label
                tp.Controls.Add(lblYear);
                //Add Columns
                gvMyOrgs.Columns.Add(orgID);
                gvMyOrgs.Columns.Add(orgName);
                gvMyOrgs.Columns.Add(cs);
                gvMyOrgs.Columns.Add(title);
                gvMyOrgs.Columns.Add(hrs);
                //Bind Data
                gvMyOrgs.DataBind();
                //Add Gridview
                tp.Controls.Add(gvMyOrgs);
               
                //Clear Parameters
                cmd.Parameters.Clear();
                //Close Datareader
                rs.Close();
                //Close Connection
                cn.Close();

            }

        }
0
Comment
Question by:cbitsupport
2 Comments
 
LVL 13

Accepted Solution

by:
agarwalrahul earned 250 total points
ID: 34913120
0
 
LVL 8

Assisted Solution

by:Anurag Agarwal
Anurag Agarwal earned 250 total points
ID: 34914758
hello !!

just check this tutorial for your help its very easy to understand http://www.dotnetfunda.com/tutorials/controls/gridview.aspx


0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now