We help IT Professionals succeed at work.

My gridview doesn't rebind in the sort event.

Medium Priority
734 Views
Last Modified: 2013-11-26
I am using Monorail with VS.Net 2005. Autogenerate columns is set to false. Sort expressions in the bound columns have been supplied. The gridview is being bound in page load event as well as the sorting event. I have 3 classes called staffing, location and personalphones. The DisplayGird() is being called in the  if (!IsPostBack). For some reason, the grid only takes the original sort parameters (the ones in the page_load), no matter which column header I click on.


private void DisplayGrid()
        {
                Staffing staffing = Staffing.FindByNetworkLogin(networklogin);
                if (staffing != null)
                {
                    lblSiteID.Text = staffing.Location.SiteID.ToString();
                    staffId = staffing.StaffId;
                    siteID = staffing.Location.SiteID;
                    lblSiteName.Text = staffing.Location.ShortName;
                    temp = this.strSortExpression + " " + this.strSortDirection;
                    //DataView dv = new DataView(PersonalPhones.GetPersonalDetails(siteID, staffId, manager, flag),"",temp,DataViewRowState.CurrentRows);
                    
                    DataView dv = PersonalPhones.GetPersonalDetails(siteID, staffId, manager, flag).DefaultView;
                    //temp = ViewState["strSortExpression"] + " " + ViewState["strSortDirection"];
                    dv.Sort = temp;
                    gvwPersonalDetails.DataSource = dv;
                    gvwPersonalDetails.DataBind();
                    
                }
        }
 
//The data table is in personalphones
 
  public static DataTable GetPersonalDetails(int siteID, int staffId, string manager, string flag)
        {
            string queryString;
            PersonalPhones[] personaldetails = null;
            DataTable table = new DataTable("personaldetails");
            DataColumn column;
            column = new DataColumn();
            column.ColumnName = "RE_Name";
            table.Columns.Add(column);
            column = new DataColumn();
            column.ColumnName = "homePhone";
            table.Columns.Add(column);
            column = new DataColumn();
            column.ColumnName = "blackBerry";
            table.Columns.Add(column);
            column = new DataColumn();
            column.ColumnName = "blackBerryPin";
            table.Columns.Add(column);
            column = new DataColumn();
            column.ColumnName = "Personal_CellPhone";
            table.Columns.Add(column);
            column = new DataColumn();
            column.ColumnName = "work_Phone";
            table.Columns.Add(column);
            column = new DataColumn();
            column.ColumnName = "ID";
            table.Columns.Add(column);
            if (manager == "True" && flag == "Y")
            {
                //personaldetails = (PersonalPhones[])FindBySiteId(siteID);
                queryString = @" select sid from PersonalPhones sid where sid.Location.SiteID =:siteID";
                SimpleQuery<PersonalPhones> q = new SimpleQuery<PersonalPhones>(queryString);
                q.SetParameter("siteID", siteID);
                personaldetails = q.Execute();
 
            }
            else
            {
                personaldetails = (PersonalPhones[])FindByStaffId(staffId);
            }
            
            foreach (PersonalPhones pp in personaldetails)
            {
                DataRow row = table.NewRow();
                row["RE_Name"] = pp.staffing.FirstName + " " + pp.staffing.LastName;
                row["homePhone"] = pp.HomePhone;
                row["blackBerry"] = pp.BlackBerry;
                row["blackBerryPin"] = pp.BlackBerryPin;
                row["Personal_CellPhone"] = pp.PersCellPhone;
                row["work_Phone"] = pp.WorkPhone;
                row["ID"] = pp.Id;
                if (pp.Staffing.Active)
                {
                    table.Rows.Add(row);
                }
            }
 
            return table;
 
        }

Open in new window

Comment
Watch Question

Senior Web Developer
CERTIFIED EXPERT
Commented:
So, remove the  if (!IsPostBack) condition from the Page_Load and always call DisplayGrid() inside Page_Load.
Because you need to bind data each time you retrieve sorted data.

Author

Commented:
I can't believe it was simple as that IsPostBack. I have been struggling for so many days. Thanks a lot! It works now

Explore More ContentExplore courses, solutions, and other research materials related to this topic.