Сортировка Gridview - Привязка к неправильной строке в режиме редактирования, когда - PullRequest
0 голосов
/ 17 марта 2020

В моем приложении ASP. NET C# Я сортирую вид сетки, и у меня также есть Редактировать в этом виде сетки, сортировка работает нормально, но когда я нажимаю кнопку Изменить, он помещает вид сетки в исходный отсортированный порядок. когда я нажимаю «Изменить» - он позволяет мне редактировать строку, но меняет порядок и показывает мне исходную запись в этой строке в режиме редактирования.

это мой код

       private string SortDirection
    {
        get { return ViewState["SortDirection"] != null ? ViewState["SortDirection"].ToString() : "ASC"; }
        set { ViewState["SortDirection"] = value; }
    }
    private void bindgrid(string sortExpression = null)
    {
        Connexion conn = new Connexion(connStr);
        using (SqlConnection con = new SqlConnection(connStr))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "selectallCentregrid";
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    using (DataTable dt = new DataTable())
                    {

                        sda.Fill(dt);
                        dt.Columns.Add("isActivee");
                        if (dt.Rows.Count > 0)
                        {
                            for (int rowIndex = 0; rowIndex < dt.Rows.Count; rowIndex++)
                            {
                                //string chemin = res.Rows[rowIndex]["Chemin"].ToString();
                                //res.Rows[rowIndex]["Chemin"] = chemin.Substring(chemin.LastIndexOf("\\") + 1);
                                if (dt.Rows[rowIndex]["isActive"].ToString().Equals("0"))
                                {
                                    dt.Rows[rowIndex]["isActivee"] = "Non";
                                }
                                if (dt.Rows[rowIndex]["isActive"].ToString().Equals("1"))
                                {
                                    dt.Rows[rowIndex]["isActivee"] = "Oui";
                                }
                            }
                        }
                        if (sortExpression != null)
                        {
                            DataView dv = dt.AsDataView();
                            this.SortDirection = this.SortDirection == "ASC" ? "DESC" : "ASC";
                            dv.Sort = sortExpression + " " + this.SortDirection;
                            gvDiplomes1.DataSource = dv;
                        }
                        else
                        {
                            gvDiplomes1.DataSource = dt;
                        }
                        gvDiplomes1.DataBind();
                    }
                }
            }
        }
    }

    protected void OnSorting(object sender, GridViewSortEventArgs e)
    {
        this.bindgrid(e.SortExpression);
    } 
    protected void gvDiplomes_RowEditing(object sender, System.Web.UI.WebControls.GridViewEditEventArgs e)
    {
        //NewEditIndex property used to determine the index of the row being edited.  
       gvDiplomes1.EditIndex = e.NewEditIndex;
       gvDiplomes1.Columns[6].Visible = true;
       gvDiplomes1.Columns[7].Visible = false;
        bindgrid();
       // RadioButtonList rblShippers = gvDiplomes1.Rows[e.RowIndex].FindControl("rblShippers") as RadioButtonList;
        this.SearchCentre();
    }
    protected void gvDiplomes_RowDataBound(object sender, GridViewRowEventArgs e)
    {

    }
    protected void gvDiplomes_RowUpdating(object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e)
    {
        //Finding the controls from Gridview for the row which is going to update  
        //TextBox Lieu_Depot = GridView1.Rows[e.RowIndex].FindControl("Txtlieu_Depot") as TextBox;
        Label IDCentre = gvDiplomes1.Rows[e.RowIndex].FindControl("lblIDCentre") as Label;

        TextBox Nom_centre = gvDiplomes1.Rows[e.RowIndex].FindControl("TxtNom_centre") as TextBox;
        TextBox Code_Centre = gvDiplomes1.Rows[e.RowIndex].FindControl("TxtCodeCentre") as TextBox;
        TextBox CapaciteMaximale = gvDiplomes1.Rows[e.RowIndex].FindControl("TxtCapaciteReel") as TextBox;
        TextBox CapaciteRepartition = gvDiplomes1.Rows[e.RowIndex].FindControl("TxtCapacite") as TextBox;
        TextBox Adresse_centre = gvDiplomes1.Rows[e.RowIndex].FindControl("TxtAdresse_centre") as TextBox;
        Label IsActivee = gvDiplomes1.Rows[e.RowIndex].FindControl("lblisActivee") as Label;
        RadioButtonList rblShippers = gvDiplomes1.Rows[e.RowIndex].FindControl("rblShippers") as RadioButtonList;

        int CapaciteRepartitiontext = Int32.Parse(CapaciteRepartition.Text);
        int CapaciteMaximaletext = Int32.Parse(CapaciteMaximale.Text);

        Connexion conn = new Connexion(connStr);
        SqlConnection con = new SqlConnection(connStr);

        if (CapaciteRepartitiontext > CapaciteMaximaletext)
        {
            lblmsg.Text = "&nbsp;&nbsp; * Capacité répartition doit être inférieure a capacité maximale";
            lblmsg.ForeColor = System.Drawing.Color.Red;
        }
        else
        {

        //con = new SqlConnection(cs);
        //con.Open();
        //updating the record  
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "updateallcentre";
        cmd.Connection = con;
        cmd.Parameters.AddWithValue("IDCentre",IDCentre.Text);
        cmd.Parameters.AddWithValue("Centre", Nom_centre.Text);
        cmd.Parameters.AddWithValue("CodeCentre", Code_Centre.Text.Trim());
        cmd.Parameters.AddWithValue("CapaciteMaximale", CapaciteMaximale.Text.Trim());
        cmd.Parameters.AddWithValue("CapaciteRepartition", CapaciteRepartition.Text.Trim());
        cmd.Parameters.AddWithValue("AdresseCentre", Adresse_centre.Text);
        cmd.Parameters.AddWithValue("isActive", rblShippers.SelectedValue.Trim());
        //  cmd.Parameters.AddWithValue("lbcommande", lblcommande.Text);
        con.Open();
        // gvDiplomes.EmptyDataText = "No records founds ";
        gvDiplomes1.DataSource = cmd.ExecuteReader();
        gvDiplomes1.EditIndex = -1;

        gvDiplomes1.Columns[6].Visible = false;
        gvDiplomes1.Columns[7].Visible = true;

        bindgrid();
        this.SearchCentre();
        lblmsg.Text = "&nbsp;&nbsp; ✓ Le centre a été bien modifié ";
        lblmsg.ForeColor = System.Drawing.Color.Green;
                    }

    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...