搜索值时 Gridview 丢失编辑索引

Gridview loses edit index when value is searched

有一个文本框可以从 Gridview 中搜索值。当值显示时,每次我单击“编辑”时,它都会转到 Gridview 的第一个索引行。我只想编辑已按 ID 搜索的行。 例如,如果我搜索第 8 行中的值。它显示第 8 行,这很好,但是当我单击“编辑”时,它会再次转到第一行。为什么会这样?

 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString);

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {

        BindGridView();

    }
}

 protected void BindGridView()
{
    DataTable dt = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter("select * from tblInventory", con);
    con.Open();
    da.Fill(dt);

    con.Close();

    if (dt.Rows.Count > 0)
    {

        GridView1.DataSource = dt;

        GridView1.DataBind();

    }

}

 protected void btnSearch_Click(object sender, EventArgs e)
{

    SqlDataAdapter da = new SqlDataAdapter("select * from tblInventory where (Part like '%" + txtSearch.Text + "%') or (Brand like '%" + txtSearch.Text + "%' )", con);
    DataSet ds = new DataSet();
    da.Fill(ds);

    GridView1.DataSource = ds;
    GridView1.DataBind();


}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
    TextBox Part = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtPart");
    TextBox Description = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtDescription");
    TextBox Qty = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtQty");
    DropDownList Brand = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlBrand");

    TextBox ItemType = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtItemType");
    SqlCommand cmd = new SqlCommand("update tblInventory set Part='" + Part.Text + "',Description='" + Description.Text + "',Qty='" + Qty.Text + "',Brand='" + Brand.Text + "',ItemType='" + ItemType.Text + "' where ID=" + id, con);
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
    GridView1.EditIndex = -1;
    BindGridView();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    GridView1.EditIndex = -1;
    BindGridView();
}
   protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView1.EditIndex = e.NewEditIndex;
    BindGridView();

}

您需要修改您的 BindGridView 方法,它会解决问题。 现在,当您在任何行中搜索一个值并显示结果时,单击编辑时,它将保留在选定的行中。

txtSearch 只是您的搜索文本框的 ID。

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
    BindGridView(this.txtSearch.Text);


}
}

protected void BindGridView(string column1)
{

SqlCommand cmd = new SqlCommand("select * from table1 where (column1 like '%" + txtSearch.Text + "%')", con);
con.Open();
cmd.Parameters.AddWithValue("@column1 ", column1 );
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
con.Close();

}

protected void btnSearch_Click(object sender, EventArgs e)
{
BindGridView(this.txtSearch.Text);

}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGridView(this.txtSearch.Text);

}

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindGridView(this.txtSearch.Text);
}