Gridview在编辑按钮上排序c#单击“不工作”

时间:2018-06-11 10:31:51

标签: c# sorting gridview

我单击编辑按钮而不在DataGridView中排序我的列标题,这很好用,我可以在下拉列表中编辑行和值。

然而,当我对列进行排序然后点击编辑时 - 它允许我编辑原始DataGridView而不是排序版本。

我认为这是一个在编辑按钮上绑定datagridview的问题,因为它没有在任何地方存储排序顺序,但似乎无法弄清楚如何保存排序会话并将其应用于GetgvCaseListData()方法。

以下代码在排序前工作正常,但在GridView排序并单击编辑按钮时中断:

protected void GetgvCaseListData()
{
    string connectionstring = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
    SqlConnection cn = new SqlConnection(connectionstring);
    SqlCommand cmd = new SqlCommand("[dbo].cr_fe_list", cn); 
    cmd.Parameters.AddWithValue("@subteamno", SubTeam);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    cmd.CommandType = CommandType.StoredProcedure;

    DataTable ds = new DataTable();
    da.Fill(ds);

    if (ds.Rows.Count > 0)
    {
        gvCaseList.DataSource = ds;
        gvCaseList.DataBind();
        ViewState["dirState"] = ds;
        ViewState["sortdr"] = "Asc";

    }
    else
    {
        noresultsvalidation.Visible = true;
        gvCaseList.DataSource = ds;
        gvCaseList.DataBind();
        ViewState["dirState"] = ds;
        ViewState["sortdr"] = "Asc";
    }

protected void gvCaseList_RowDataBound(object sender, GridViewRowEventArgs e)
{

    MeasurementID = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "MeasurementID"));
    string connectionstring = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
    SqlConnection cn = new SqlConnection(connectionstring);
    if (e.Row.Cells[10].Text == "OVERDUE")
    {
        e.Row.Cells[10].ForeColor = System.Drawing.Color.Red;
        e.Row.Cells[10].Font.Bold = true;
    }
    if (e.Row.RowType == DataControlRowType.DataRow && ((e.Row.RowState & DataControlRowState.Edit) > 0))
    {
        cn.Open();
        DropDownList DropDownList1 = (e.Row.FindControl("ddlException") as DropDownList);
        TextBox tb1 = (e.Row.FindControl("ExceptionText") as TextBox);
        SqlCommand cmd = new SqlCommand("[dbo].cr_fe_exception", cn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@SubTeamNo", SubTeam);
        cmd.Parameters.AddWithValue("@MeasurementID", MeasurementID);

        DataTable dt = new DataTable();
        using (SqlDataAdapter a = new SqlDataAdapter(cmd))
        {
            a.Fill(dt);
        }

        cn.Close();
        DropDownList1.DataSource = dt;
        DropDownList1.DataTextField = "ExceptionDesc";
        DropDownList1.DataValueField = "ExceptionID";
        DropDownList1.DataBind();
        DropDownList1.Items.Insert(0, new ListItem("--Select Exception--", "0"));


        if (CurrentException != "")
        {

            DropDownList1.SelectedValue = DropDownList1.Items.FindByText(CurrentException).Value;
            tb1.Enabled = true;

        }
    }
}

protected void gvCaseList_edit(object sender, GridViewEditEventArgs e)
{

    Label label1 = (Label)gvCaseList.Rows[e.NewEditIndex].FindControl("Label1");
    CurrentException = label1.Text;

    gvCaseList.EditIndex = e.NewEditIndex;
    GetgvCaseListData();

}

protected void gvCaseList_Sorting(object sender, GridViewSortEventArgs e)
{
    DataTable dtrslt = (DataTable)ViewState["dirState"];
    if (dtrslt.Rows.Count > 0)
    {
        if (Convert.ToString(ViewState["sortdr"]) == "Asc")
        {
            dtrslt.DefaultView.Sort = e.SortExpression + " Desc";
            ViewState["sortdr"] = "Desc";
        }
        else
        {
            dtrslt.DefaultView.Sort = e.SortExpression + " Asc";
            ViewState["sortdr"] = "Asc";
        }
        gvCaseList.DataSource = dtrslt;
        gvCaseList.DataBind();

    }  

}

0 个答案:

没有答案