2014-01-08 37 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

标记有用,如果它帮助你 –