2014-01-08 72 views
0

我已經嘗試了很多次,但我無法刪除網格視圖中的一行。我沒有創建任何數據庫。我只是將所有文本字段的值存儲在GRID VIEW中。如果我想使用模板字段按鈕的方式刪除GRID VIEW中的一行,那麼解決方案是什麼。如何使用模板字段在gridview中刪除一行

這是我在GridView中存儲和填充值的方式。

DataSet ds = new DataSet(); 
DataRow dr = ds.Tables[0].NewRow(); 
dr[0] = lbltxtcustomer.Text; 
dr[1] = FNtxt.Text; 
dr[2] = LNtxt.Text; 
dr[3] = DrpdownMonth.Text + "/" + DrpdownDay.Text + "/" + DrpdownYear.Text; 
dr[4] = lbltxtage.Text; 
dr[5] = txtEmail.Text; 
dr[6] = TxtPhone.Text; 
dr[7] = Txtlocation.Text; 
ds.Tables[0].Rows.Add(dr); 
BindGrid() 

;

+0

你必須做出動態的數據表格來存儲gridview中的值,這是否太需要一些代碼來解釋... –

+0

http:// dotprogramming .blogspot.com/2013/10/how-to-delete-multiple-rows-from.html –

+0

http://www.codeproject.com/Questions/267426/how-to-delete-the-gridview-row-with -templatefield –

回答

0

嘗試這個

HTML標記

下面是APS.Net GridView控件的HTML標記。這裏我使用CommandField和OnRowDeleting事件來刪除GridView Row。因此,我會將JavaScript確認框應用到CommandFieldDelete按鈕本身。

<asp:GridView ID="GridView1" CssClass = "Grid" runat="server" OnRowDeleting="OnRowDeleting" AutoGenerateColumns = "false" OnRowDataBound = "OnRowDataBound"> 
    <Columns> 
     <asp:BoundField DataField="Item" HeaderText="Item" /> 
     <asp:BoundField DataField="Price" HeaderText="Price" /> 
     <asp:CommandField ShowDeleteButton="True" ButtonType="Button" /> 
    </Columns> 
</asp:GridView> 

運用JavaScript的確認框到GridView CommandField中 刪除按鈕

要應用的JavaScript確認框,我正在尋找,因爲在GridView細胞指數2的控制按鈕它有CommandField。一旦

protected void OnRowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     string item = e.Row.Cells[0].Text; 
     foreach (Button button in e.Row.Cells[2].Controls.OfType<Button>()) 
     { 
      if (button.CommandName == "Delete") 
      { 
       button.Attributes["onclick"] = "if(!confirm('Do you want to delete " + item + "?')){ return false; };"; 
      } 
     } 
    } 
} 

使用CommandField中和OnRowDeleting 事件

下面是代碼的ASP.Net的GridView行使用OnRowDeleting事件刪除刪除ASP.Net的GridView行

protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e) 
{ 
    int index = Convert.ToInt32(e.RowIndex); 
    DataTable dt = ViewState["dt"] as DataTable; 
    dt.Rows[index].Delete(); 
    ViewState["dt"] = dt; 
    BindGrid(); 
} 
+0

@Boopathi對此沒有任何想法,但我認爲這不好 –

+0

標記有用,如果它幫助你 –