GridView编辑,删除,更新不起作用?

时间:2013-10-13 16:10:08

标签: c# asp.net gridview

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

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

图像 http://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();
    }

2 个答案:

答案 0 :(得分:0)

您必须修改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 DataSource应为null。为了避免这种情况,我会改变代码:

//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();
    }
}

答案 1 :(得分:0)

我认为您需要在页面加载事件的!ISPostbak内部添加绑定gridview方法。

请参阅此代码

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

}