sorting doesn't work when editing gridview's row

 Hi,

 I have implemented a GridView's sorting and editing manually and they both work seperately, but not together. If I sort then try to edit one of the sorted records, the GridView edits a row on the first page that corresponds to the location of the edited row. In other words, the page loses sorting, when the edit button is clicked and the wrong row is edited.

Any suggestions will be very much appreciated.

E

0
Ennair
10/16/2008 11:15:22 AM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

2 Replies
1660 Views

Similar Articles

[PageSpeed] 42

Hi Ennair ,

Here is one sample,

 

    <form id="form1" runat="server">
    <div>
       <asp:GridView ID="gvUsers" runat="server" AllowPaging="True" AllowSorting="True" 
            OnPageIndexChanging="gridView_PageIndexChanging" OnSorting="gridView_Sorting" 
            AutoGenerateColumns="False" OnDataBound="gvUsers_DataBound" 
            OnRowDataBound="gvUsers_RowDataBound" OnRowDeleting="gvUsers_RowDeleting" 
            DataKeyNames="age" PageSize="2">
        <columns>
            <asp:buttonfield buttontype="Button" 
            commandname="Edit"
            headertext="Edit" 
            text="E" />
            
            <asp:buttonfield buttontype="Button" 
            commandname="Delete"
            headertext="Delete" 
            text="D" />
            <asp:TemplateField HeaderText="templateAge" SortExpression="age">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("age") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="templateName" SortExpression="name">
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Eval("name") %>'></asp:Label>
                    <asp:Label ID="Label3" runat="server" Text='<%# Eval("age") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </columns >
          
</asp:GridView> 
    </div>
    </form>
  
       protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                bind();
        }

        private void bind()
        {
            DataTable table = getTable();

            this.gvUsers.DataSource = table;
            gvUsers.DataBind();

        }

        private DataTable getTable()
        {
            DataTable table = new DataTable();
            table.Columns.Add("age");
            table.Columns.Add("name");
            DataRow dr = table.NewRow();
            dr["age"] = 1;
            dr["name"] = "zz";
            table.Rows.Add(dr);

            DataRow dr2 = table.NewRow();
            dr2["age"] = 2;
            dr2["name"] = "vv";
            table.Rows.Add(dr2);

            DataRow dr3 = table.NewRow();
            dr3["age"] = 3;
            dr3["name"] = "gg";
            table.Rows.Add(dr3);

            return table;
        }

        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.gvUsers.PageIndex = e.NewPageIndex;
            if (GridViewSortDirection == SortDirection.Ascending)
            {
                SortGridView(SortExpression, "asc");
            }
            else
            {
                SortGridView(SortExpression, "desc");
            }
            
        }

        protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sortExpression = e.SortExpression;
            ViewState["z_sortexpresion"] = e.SortExpression;
            if (GridViewSortDirection == SortDirection.Ascending)
            {
                GridViewSortDirection = SortDirection.Descending;
                SortGridView(sortExpression, "DESC");
            }
            else
            {
                GridViewSortDirection = SortDirection.Ascending;
                SortGridView(sortExpression, "ASC");
            }

        }

        public string SortExpression
        {
            get
            {
                if (ViewState["z_sortexpresion"] == null)
                    ViewState["z_sortexpresion"] = this.gvUsers.DataKeyNames[0].ToString();
                return ViewState["z_sortexpresion"].ToString();
            }
            set
            {
                ViewState["z_sortexpresion"] = value;
            }
        }

        public SortDirection GridViewSortDirection
        {
            get
            {
                if (ViewState["sortDirection"] == null)
                    ViewState["sortDirection"] = SortDirection.Ascending;
                return (SortDirection)ViewState["sortDirection"];
            }
            set
            {
                ViewState["sortDirection"] = value;
            }
        }

        protected void gvUsers_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void gvUsers_DataBound(object sender, EventArgs e)
        {
            int i = this.gvUsers.Columns.Count;
            this.gvUsers.Columns[2].Visible = false;



        }

        protected void gvUsers_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label toHide = (Label)e.Row.FindControl("Label3");

                toHide.Attributes.Add("style", "display:none");
            }
        }

        private void SortGridView(string sortExpression, string direction)
        {
            DataTable dt = getTable();
            DataView dv = new DataView(dt);
            //dv.Sort = "age asc";
            dv.Sort = sortExpression + " " + direction;
            this.gvUsers.DataSource = dv;
            gvUsers.DataBind();
        }
 

Samu Zhang
Microsoft Online Community Support

Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
0
Samu
10/20/2008 5:58:38 AM

 Get the ID of the Sorting record and edit the record, or else it will edit the previous or unsorted value. Please remember whenever you edit a record, get the ID and then correcponding Grid_Index and then edit.

Can you post your code here please?


Cheers,
Achutha Krishnan

~ No one can do everything, but everyone can do something ~
0
engineerachu
10/20/2008 9:11:47 AM
Reply: