网格视图编辑,删除,更新不起作用

本文关键字:更新 不起作用 删除 网格 编辑 视图 | 更新日期: 2023-09-27 18:35:14

im 将一些发票 没有形成一个表格到下拉列表,选择发票编号后,我可以将所有产品带到网格视图,附加 , 价格和所有,

我的问题是我必须单击两次才能编辑该行,但它没有更新,当第一次单击第一行中的编辑按钮时,它不起作用,但是单击下一行后,编辑按钮第一行正在启用编辑文本框,但不只更新网格上的空数据......

图像https://i.stack.imgur.com/X8W6Q.gif

在这一部分中,我将数据带到网格..

    DataTable DataTbt = new DataTable();
    SqlCommand Command = new SqlCommand();
    SqlDataAdapter DtaAdapter = new SqlDataAdapter();
    protected void ddlInvoiceNumber_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            GetReturnRecords("Invoice No", ddlInvoiceNumber.SelectedValue);
        }
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", String.Format("alert('Error occured : {0}');", ex.Message), true);
        }
        finally
        {
            DataTbt.Clear();
            DataTbt.Dispose();
            DtaAdapter.Dispose();
            Command.Dispose();
            Connection.Close();
        }
    }
    private void GetReturnRecords(string searchBy, string searchVal)
    {
        try
        {
            Command = new SqlCommand("SP_SearchPurchasesLines", Connection);
            Command.CommandType = CommandType.StoredProcedure;
            Command.Parameters.AddWithValue("@SearchBy", searchBy);
            Command.Parameters.AddWithValue("@SearchVal", searchVal);
            DtaAdapter.SelectCommand = Command;
            DtaAdapter.Fill(DataTbt);
            if (DataTbt.Rows.Count > 0)
            {
                GridViewPurchaseReturn.DataSource = DataTbt;
                GridViewPurchaseReturn.DataBind();
            }
            else
            {
                GridViewPurchaseReturn.DataSource = DataTbt;
                GridViewPurchaseReturn.DataBind();
            }
        }
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", String.Format("alert('Error occured : {0}');", ex.Message), true);
        }
        finally
        {
            DataTbt.Clear();
            DataTbt.Dispose();
            Command.Dispose();
            Connection.Close();
        }
    }

数据绑定到网格。

    private void BindReturnGrid()
    {
        try
        {
            DtaAdapter = new SqlDataAdapter("SP_SearchPurchasesLines", Connection);
            DtaAdapter.Fill(DataTbt);
            if (DataTbt.Rows.Count > 0)
            {
                GridViewPurchaseReturn.DataSource = DataTbt;
                GridViewPurchaseReturn.DataBind();
            }
            else
            {
                GridViewPurchaseReturn.DataSource = null;
                GridViewPurchaseReturn.DataBind();
            }
        }
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Error occured : " + ex.Message.ToString() + "');", true);
        }
        finally
        {
            DataTbt.Clear();
            DataTbt.Dispose();
            DtaAdapter.Dispose();
            Connection.Close();
        }
    }

编辑更新从网格中删除行...

    protected void GridViewPurchaseReturn_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridViewPurchaseReturn.EditIndex = -1;
        BindReturnGrid();
    }
    protected void GridViewPurchaseReturn_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridViewPurchaseReturn.EditIndex = e.NewEditIndex;
        BindReturnGrid();
    }

网格视图编辑,删除,更新不起作用

我认为您需要在内部添加绑定网格视图方法!ISPostbak on page load event .

查看此代码

page-load ()
{
if(!IsPostback)
{
BindReturnGrid();
}
}
您必须

修改GridViewPurchaseReturn_RowUpdatingGridViewPurchaseReturn_RowDeleting方法来更新更改:

protected void GridViewPurchaseReturn_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
   //Your code to delete the records in db        
    BindReturnGrid();
}


protected void GridViewPurchaseReturn_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    //Your code to update the records in db
    GridViewPurchaseReturn.EditIndex = -1;
    BindReturnGrid();
}

编辑:释放命令并关闭连接后,我认为您的 GridView 数据源应该为空。为了避免这种情况,我将像这样更改代码:

//DataTable DataTbt = new DataTable();
//SqlCommand Command = new SqlCommand();
//SqlDataAdapter DtaAdapter = new SqlDataAdapter();
protected void ddlInvoiceNumber_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        GetReturnRecords("Invoice No", ddlInvoiceNumber.SelectedValue);
    }
    catch (Exception ex)
    {
        ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", String.Format("alert('Error occured : {0}');", ex.Message), true);
    }
    finally
    {
        //DataTbt.Clear();
        //DataTbt.Dispose();
        //DtaAdapter.Dispose();
        //Command.Dispose();
        //Connection.Close();
    }
}
private void GetReturnRecords(string searchBy, string searchVal)
{
    DataTable DataTbt = new DataTable();
    SqlDataAdapter DtaAdapter = new SqlDataAdapter();
    try
    {
        SqlCommand Command = new SqlCommand("SP_SearchPurchasesLines", Connection);
        Command.CommandType = CommandType.StoredProcedure;
        Command.Parameters.AddWithValue("@SearchBy", searchBy);
        Command.Parameters.AddWithValue("@SearchVal", searchVal);
        using (Connection)
        {
            Connection.Open();
            DtaAdapter.SelectCommand = Command;
            DtaAdapter.Fill(DataTbt);
        }
        if (DataTbt.Rows.Count > 0)
        {
            GridViewPurchaseReturn.DataSource = DataTbt;
            GridViewPurchaseReturn.DataBind();
        }
        else
        {
            GridViewPurchaseReturn.DataSource = DataTbt;
            GridViewPurchaseReturn.DataBind();
        }
    }
    catch (Exception ex)
    {
        ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", String.Format("alert('Error occured : {0}');", ex.Message), true);
    }
    finally
    {
        //DataTbt.Clear();
        //DataTbt.Dispose();
        //Command.Dispose();
        //Connection.Close();
    }
}
private void BindReturnGrid()
{
    try
    {
        DataTable DataTbt = new DataTable();
        SqlDataAdapter DtaAdapter = new SqlDataAdapter("SP_SearchPurchasesLines", Connection);
        using (Connection)
        {
            Connection.Open();
            DtaAdapter.Fill(DataTbt);
        }
        if (DataTbt.Rows.Count > 0)
        {
            GridViewPurchaseReturn.DataSource = DataTbt;
            GridViewPurchaseReturn.DataBind();
        }
        else
        {
            GridViewPurchaseReturn.DataSource = null;
            GridViewPurchaseReturn.DataBind();
        }
    }
    catch (Exception ex)
    {
        ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Error occured : " + ex.Message.ToString() + "');", true);
    }
    finally
    {
        //DataTbt.Clear();
        //DataTbt.Dispose();
        //DtaAdapter.Dispose();
        //Connection.Close();
    }
}