Solved

Sorting and paging in a dynamic gridview control.

Posted on 2011-02-16
2
741 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 AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

772 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