If the database is not deleted, only one row of the GridView is deleted!

Source: Internet
Author: User

If the database is not deleted, only one row of the gridview is deleted!

In fact, you only need to delete a row in the retrieved able or dataset and then bind the data source.

However, do not execute dataset acceptchanges () because it can synchronize data changes in databases and dataset.

<Asp: gridview id = "view1" runat = "server" onrowcommand = "gridview1_rowcommand">
<Columns>
<Asp: templatefield headertext = "excluding">
<Itemtemplate>
<Asp: button id = "btndelete" runat = "server" cssclass = "button1" text = "Remove" commandname = "Del"
Commandargument = '<% # eval ("ID") %> 'onclientclick = "return window. Confirm (' Are you sure you want to divide? '); "/>
</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 ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.