我已经尝试了很多次,但我无法在GRID VIEW中删除一行。我没有创建任何数据库。我只是将文本字段的所有值存储在GRID VIEW中。如果我想使用模板字段按钮方式删除GRID VIEW中的行,那将是什么解决方案。
这是我在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 :(得分:0)
试试这个
HTML标记
以下是APS.Net GridView的HTML标记。这里我使用CommandField和OnRowDeleting事件来删除GridView Row。因此,我将JavaScript Confirmation Box应用于CommandFieldDelete Button本身。
<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 Cell Index 2的控件中寻找Button,因为它有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 Row的代码
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();
}