locked
Problem in updating gridview after sorting RRS feed

  • Question

  • User-564804143 posted

    Hello friends..

    i am sorting and updating grid view. prblem is:

    1. on edit row index gets change after sort grid by any column and upadate is not working.

    2. i gave link in id and opening a details of member for that id into fancy. it is not working after sorting.

    <asp:UpdatePanel ID="UpdatePanel2" runat="server">
          <ContentTemplate>
                <asp:GridView ID="GridDispatchshow" DataKeyNames="Dispatchid,MemberShipid" runat="server"
                    AutoGenerateColumns="False" Width="100%" PageSize="100" AllowPaging="True" OnPageIndexChanging="GridDispatchshow_PageIndexChanging"
                    OnRowCancelingEdit="GridDispatchshow_RowCancelingEdit" OnRowEditing="GridDispatchshow_RowEditing"
                    OnSorting="GridDispatchshow_Onsorting" OnRowUpdating="GridDispatchshow_RowUpdating"
                    OnRowDataBound="GridDispatchshow_RowDataBound" CssClass="memberDetailTab" AllowSorting="true">
                    <RowStyle ForeColor="#000066" />
                    <Columns>
                        <asp:TemplateField HeaderText="MembershipId" SortExpression="MemberShipId" HeaderStyle-Width="90">
                            <ItemStyle />
                            <ItemTemplate>
                                <asp:HyperLink ID="hrfMemberHistory" Width="90" CssClass="Hisfancy" runat="server"
                                    Text='<%#Bind("MemberShipid") %>' ToolTip="Click to get Member Detail"></asp:HyperLink><br />
                                <br />
                            </ItemTemplate>
                            <HeaderStyle Width="60px"></HeaderStyle>
                        </asp:TemplateField>
                        <asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" SortExpression="Name"
                            ControlStyle-CssClass="textboxtransform" HtmlEncode="false" DataFormatString="&lt;div style=&quot;text-transform:capitalize&quot;&gt;{0}&lt;/div&gt;" />
                        <asp:TemplateField HeaderText="Mobile">
                            <ItemTemplate>
                                <asp:Label ID="lblMobile1" runat="server" Text='<%# Bind("Mobile1") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="Address" HeaderText="Address" ReadOnly="True" />
                        <asp:BoundField DataField="City" HeaderText="City" ReadOnly="True" SortExpression="City"
                            ControlStyle-CssClass="textboxtransform" HtmlEncode="false" DataFormatString="&lt;div style=&quot;text-transform:capitalize&quot;&gt;{0}&lt;/div&gt;" />
                        <asp:BoundField DataField="State" HeaderText="State" ReadOnly="True" SortExpression="State"
                            ControlStyle-CssClass="textboxtransform" HtmlEncode="false" DataFormatString="&lt;div style=&quot;text-transform:capitalize&quot;&gt;{0}&lt;/div&gt;" />
                        <asp:BoundField DataField="DispatchMode" HeaderText="Dispatch Mode" ReadOnly="True"
                            SortExpression="DispatchMode" />
                        <asp:BoundField DataField="Preferences" HeaderText="Preferences" ReadOnly="true"
                            SortExpression="Preferences" />
                        <asp:TemplateField HeaderText="AWB No." SortExpression="AWBNo">
                            <EditItemTemplate>
                                <asp:TextBox ID="awb" runat="server" Text='<%# Bind("AWBNo") %>'></asp:TextBox>
                                <asp:RequiredFieldValidator ID="RequiredAWB" runat="server" ControlToValidate="awb"
                                    ErrorMessage="Required" ValidationGroup="edit" Display="Dynamic"></asp:RequiredFieldValidator>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="lblAWBNo" runat="server" Text='<%# Bind("AWBNo") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Dispatch Date" SortExpression="DispatchDate">
                            <EditItemTemplate>
                                <asp:TextBox ID="Dispatchdate" runat="server" onfocus="showCalendarControl(this);"
                                    Text='<%# Bind("DispatchDate") %>'></asp:TextBox>
                                <asp:RequiredFieldValidator ID="RequiredDD" runat="server" ControlToValidate="Dispatchdate"
                                    ErrorMessage="Required" ValidationGroup="edit" Display="Dynamic"></asp:RequiredFieldValidator>
                                <ajaxcontrol:CalendarExtender ID="CE_Dispatchdate" runat="server" Enabled="True"
                                    Format="dd/MM/yyyy" PopupPosition="Left" TargetControlID="Dispatchdate">
                                </ajaxcontrol:CalendarExtender>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="lblDispatchdate" runat="server" Text='<%# Bind("DispatchDate") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <%-- <asp:TemplateField HeaderText="Address Invalid" SortExpression="AddressInvalid">
                            <EditItemTemplate>
                                <asp:DropDownList ID="ddladdressinvalid" runat="server" OnSelectedIndexChanged="ddladdressinvalid_SelectedIndexChanged"
                                    AutoPostBack="true">
                                    <asp:ListItem>False</asp:ListItem>
                                    <asp:ListItem>True</asp:ListItem>
                                </asp:DropDownList>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="lbladdressinvalid" runat="server" Text='<%# Bind("AddressInvalid") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>--%>
                        <asp:TemplateField HeaderText="Problem faced" SortExpression="Problemfaced">
                            <EditItemTemplate>
                                <asp:DropDownList ID="ddlpblmfaced" runat="server" OnSelectedIndexChanged="ddlpblmfaced_SelectedIndexChanged">
                                    <asp:ListItem>False</asp:ListItem>
                                    <asp:ListItem>True</asp:ListItem>
                                </asp:DropDownList>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="lblproblemfaced" runat="server" Text='<%# Bind("Problemfaced") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Remark">
                            <EditItemTemplate>
                                <asp:TextBox ID="Txtremark" runat="server" Text='<%#Bind("Remark") %>' TextMode="MultiLine"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="Requiredremark" runat="server" ControlToValidate="Txtremark"
                                    ErrorMessage="Required" ValidationGroup="edit" Display="Dynamic"></asp:RequiredFieldValidator>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="lblRemark" runat="server" Text='<%# Bind("Remark") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="ManagedBy" HeaderText="Managed By" ReadOnly="True" />
                        <asp:CommandField HeaderText="Edit Record" ShowEditButton="true" ValidationGroup="edit" />
                    </Columns>
                </asp:GridView>
           </ContentTemplate>
          <%--  <Triggers>
                <asp:AsyncPostBackTrigger ControlID="GridDispatchshow" EventName="RowEditing" />
                <asp:AsyncPostBackTrigger ControlID="GridDispatchshow" EventName="RowUpdated" />
                <asp:AsyncPostBackTrigger ControlID="GridDispatchshow" EventName="RowCancelingEdit" />
                <asp:AsyncPostBackTrigger ControlID="GridDispatchshow" EventName="Sorting" />
            </Triggers>--%>
        </asp:UpdatePanel>
     protected void GridDispatchshow_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridDispatchshow.EditIndex = e.NewEditIndex;
            Label lblpblm = (Label)GridDispatchshow.Rows[e.NewEditIndex].Cells[10].FindControl("lblproblemfaced");
            //Label lbladd = (Label)GridDispatchshow.Rows[e.NewEditIndex].Cells[11].FindControl("lblproblemfaced");
            fillgridview();
            DropDownList ddlpblm = (DropDownList)GridDispatchshow.Rows[e.NewEditIndex].Cells[10].FindControl("ddlpblmfaced");
            ddlpblm.SelectedValue = lblpblm.Text;
            //DropDownList ddladd = (DropDownList)GridDispatchshow.Rows[e.NewEditIndex].Cells[11].FindControl("ddladdressinvalid");
            //ddladd.SelectedValue = lbladd.Text;
    
            if (ViewState["SortOrder"] != null)
            {
                DataTable edt = (DataTable)ViewState["SortOrder"];
                GridDispatchshow.DataSource = edt;
                GridDispatchshow.DataBind();
                fillgridview();
            }
            else
            {
                fillgridview();
            }
        }
    
        protected void GridDispatchshow_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            con.Open();
            datautility objdataacess = new datautility();
            int dispatchid = Convert.ToInt32(GridDispatchshow.DataKeys[e.RowIndex].Values["Dispatchid"].ToString());
            string txtawb = ((TextBox)GridDispatchshow.Rows[e.RowIndex].FindControl("awb")).Text;
            string dispatchdate = ((TextBox)GridDispatchshow.Rows[e.RowIndex].FindControl("Dispatchdate")).Text;
            string problem = ((DropDownList)GridDispatchshow.Rows[e.RowIndex].FindControl("ddlpblmfaced")).SelectedValue.ToString();
            string remark = ((TextBox)GridDispatchshow.Rows[e.RowIndex].FindControl("Txtremark")).Text;
            //  string invalidadd = ((DropDownList)GridDispatchshow.Rows[e.RowIndex].FindControl("ddladdressinvalid")).SelectedValue.ToString();
            string Id = Convert.ToString(GridDispatchshow.DataKeys[e.RowIndex].Values["MemberShipid"]);
    
            // inserting new row in Memberdetails table 
    
            //if (invalidadd == "True")
            //{
            SqlCommand update = new SqlCommand("Update..", con);
            update.ExecuteNonQuery();
            DataTable dt = new DataTable();
          
            SqlDataAdapter da = new SqlDataAdapter(selectquery, con);
            da.Fill(dt);
    
    
            if (dt.Rows.Count > 0)
            {
                SqlCommand cmdinsert = new SqlCommand("insert into.....)", con);
                cmdinsert.ExecuteNonQuery();
            }
            //}
            SqlCommand cmd = new SqlCommand("update..., con);
            cmd.ExecuteNonQuery();
            con.Close();
            GridDispatchshow.EditIndex = -1;
            if (ViewState["SortOrder"] != null)
            {
                DataTable edt = (DataTable)ViewState["SortOrder"];
                GridDispatchshow.DataSource = edt;
                GridDispatchshow.DataBind();
    
            }
            else
            {
                fillgridview();
            }
            //  fillgridview();
          
        }
    
        protected void GridDispatchshow_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridDispatchshow.PageIndex = e.NewPageIndex;
            fillgridview();
        }
        protected void GridDispatchshow_Onsorting(object sender, GridViewSortEventArgs e)
        {
            string sorting = string.Empty;
            if (GridViewSortDirection == SortDirection.Ascending)
            {
                GridViewSortDirection = SortDirection.Descending;
                sorting = "Desc";
            }
            else
            {
                GridViewSortDirection = SortDirection.Ascending;
                sorting = "Asc";
            }
            DataTable dt = (DataTable)Cache["dt"];
            DataView sortedview = new DataView(dt);
            sortedview.Sort = e.SortExpression + " " + sorting;
            GridDispatchshow.DataSource = sortedview;
            GridDispatchshow.DataBind();
            ViewState["SortOrder"] = ((DataView)GridDispatchshow.DataSource).ToTable();
    
        }
        public SortDirection GridViewSortDirection
        {
            get
            {
                if (ViewState["sortDirection"] == null)
                    ViewState["sortDirection"] = SortDirection.Ascending;
                return (SortDirection)ViewState["sortDirection"];
            }
            set { ViewState["sortDirection"] = value; }
        }
    
        protected void GridDispatchshow_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
    
                    int Id = Convert.ToInt32(GridDispatchshow.DataKeys[e.Row.RowIndex].Values["MemberShipid"]);
                    HyperLink hrfMemberHistory = (HyperLink)e.Row.FindControl("hrfMemberHistory");
                    hrfMemberHistory.NavigateUrl = "~/MagazineTracking/DisplayMemberDetail.aspx?MemberShipId=" + Id;
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
            }
        }



    i am using update panel and triger :

    below is my code:

    Monday, April 8, 2013 5:01 AM

Answers

All replies