删除网格视图中的特定行
本文关键字:网格 视图 删除 | 更新日期: 2023-09-27 17:58:56
在下面的代码中,我有一个网格视图,其中有6列,其中有1个dropdown
和5个textbox
。我的目标是删除特定的行。我尝试过,但无法删除特定的列。
protected void gvInvoice_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
DataTable dtCurrentTable = new DataTable();
int RowIndex = e.RowIndex;
dtCurrentTable = (DataTable)ViewState["CurrentTable"];
dtCurrentTable.Rows.RemoveAt(e.RowIndex);
SetPreviousData1(dtCurrentTable);
}
private void SetPreviousData1(DataTable dtCurrentTable)
{
int rowIndex = 0;
if (dtCurrentTable != null)
{
DataTable dt = dtCurrentTable;
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
Label lblProductID = (Label)gvInvoice.Rows[rowIndex].Cells[1].FindControl("lblProductID_i");
DropDownList txtProductName = (DropDownList)gvInvoice.Rows[rowIndex].Cells[2].FindControl("EditableddlProductName");
TextBox txtQuantity = (TextBox)gvInvoice.Rows[rowIndex].Cells[3].FindControl("txtQuantity");
TextBox txtProductPrices = (TextBox)gvInvoice.Rows[rowIndex].Cells[4].FindControl("lblProductPrices");
TextBox txtProfitPrice = (TextBox)gvInvoice.Rows[rowIndex].Cells[5].FindControl("txtProfitPrice");
TextBox txtTaxCalculation = (TextBox)gvInvoice.Rows[rowIndex].Cells[6].FindControl("txtTaxCalculation");
TextBox txtTotaPrice = (TextBox)gvInvoice.Rows[rowIndex].Cells[7].FindControl("txtTotaPrice");
lblProductID.Text = dt.Rows[i]["Column1"].ToString();
txtProductName.Text = dt.Rows[i]["Column2"].ToString();
txtQuantity.Text = dt.Rows[i]["Column3"].ToString();
txtProductPrices.Text = dt.Rows[i]["Column4"].ToString();
txtProfitPrice.Text = dt.Rows[i]["Column5"].ToString();
txtTaxCalculation.Text = dt.Rows[i]["Column6"].ToString();
txtTotaPrice.Text = dt.Rows[i]["Column7"].ToString();
rowIndex++;
}
}
}
}
我认为您需要再次绑定gridview数据源。
gvInvoice.DataSource = dtCurrentTable;
gvInvoice.DataBind()
将此代码放在删除数据表之后
您正在从网格视图中删除该行,但随后您将再次调用databind,这只是将网格视图刷新到原始数据源所在的状态。
从数据源中删除它,然后进行数据绑定,或者在不进行密文绑定的情况下进行数据绑定并从网格视图中删除它。
protected void gvInvoice_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
myobj.myconnection();// connection created
string mystr = "Delete table_name where water_id= '" + gvInvoice.DataKeys[e.RowIndex].Value + "'";// query
sqlcmd = new SqlCommand(mystr, myobj.mycon);
sqlcmd.ExecuteNonQuery();
fillgrid();
}
您必须更新您的Datebase,尝试给定delete存储过程并应用于此网格数据源。