Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Sorting and paging in a dynamic gridview control.

Posted on 2011-02-16
2
Medium Priority
?
754 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

610 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