2014-04-17 68 views
0

在下面的代碼中,我有網格視圖,其中有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++; 
      } 
     } 
    } 
} 

回答

1

我想你需要再次綁定gridview數據源。

gvInvoice.DataSource = dtCurrentTable; 
    gvInvoice.DataBind() 

地方刪除的數據表

+0

謝謝它運作良好 – Thiyagu

0

您刪除在GridView的行,但隨後你會並再次調用數據綁定這是剛剛刷新GridView控件相同的狀態,原來的數據源是此代碼後in。

將其從數據源中刪除,然後將數據綁定或數據綁定從gridview中刪除而不進行重新綁定。

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

您必須更新您的數據庫嘗試給予刪除存儲過程並應用於此網格數據源。