?
Solved

Sorting and paging in a dynamic gridview control.

Posted on 2011-02-16
2
Medium Priority
?
749 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 13

Accepted Solution

by:
Rahul Agarwal earned 1000 total points
ID: 34913120
0
 
LVL 8

Assisted Solution

by:Anurag Agarwal
Anurag Agarwal earned 1000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

764 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