概述
<asp:GridView ID="view1" runat="server" OnRowCommand="gridview1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="刪除">
<ItemTemplate>
<asp:Button ID="btnDelete" runat="server" CssClass="button1" Text="刪除" CommandName="Del"
CommandArgument=' <%#Eval("ID") %>' OnClientClick="return window.confirm('您確定要刪除嗎?');" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("ID"));
dt.Columns.Add(new DataColumn("name"));
DataRow dr = dt.NewRow();
dr["ID"] = 1;
dr["name"] = "xx";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["ID"] = 2;
dr["name"] = "yy";
dt.Rows.Add(dr);
ViewState["DT"] = dt;
DataBind();
}
}
private void DataBind()
{
view1.DataSource = ViewState["DT"];
view1.DataBind();
}
protected void gridview1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Del")
{
int id = Convert.ToInt32(e.CommandArgument);
GridViewRow gvrow = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
int index = gvrow.RowIndex;
DataTable dt = (DataTable)ViewState["DT"];
dt.Rows.RemoveAt(index);
DataBind();
}
最后
以上就是自觉枫叶为你收集整理的GridView 删除某一行,只去除GridView中的记录,不更新数据库的全部内容,希望文章能够帮你解决GridView 删除某一行,只去除GridView中的记录,不更新数据库所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复