Solved

Sorting and paging in a dynamic gridview control.

Posted on 2011-02-16
2
740 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Programmatically signing Word macros 4 61
How do ASP.NET and MVC work together? 4 25
Error in page 3 45
Ajax Panel Not Updating When Triggered By Timer 4 37
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
A short film showing how OnPage and Connectwise integration works.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

914 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

23 Experts available now in Live!

Get 1:1 Help Now