| |

VerySource

 Forgot password?
 Register
Search
View: 889|Reply: 4

Delete database records under datagrid

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-2-19 18:00:01
| Show all posts |Read mode
I defined the following datagrid in .aspx, where the asp: ButtonColumn column is used to delete data.
<asp: datagrid id = "datagrid1" runat = "server" AllowPaging = "True" PageSize = "8" AutoGenerateColumns = "False" DataKeyField = "zt_No" Width = "100%" EnableViewState = "false" PagerStyle-Visible = " false "OnDeleteCommand =" datagrid1_DeleteCommand "OnPageIndexChanged =" datagrid1_PageIndexChanged ">
<EditItemStyle Width = "3px" BackColor = "# FFFF80"> </ EditItemStyle>
<Columns>
<asp: BoundColumn DataField = "zt_No" SortExpression = "0" ReadOnly = "True" HeaderText = "number"> </ asp: BoundColumn>
<asp: BoundColumn DataField = "zt_name" SortExpression = "2" ReadOnly = "True" HeaderText = "Name"> </ asp: BoundColumn>
<asp: BoundColumn DataField = "zt_sex" ReadOnly = "True" HeaderText = "Gender"> </ asp: BoundColumn>
<asp: HyperLinkColumn DataNavigateUrlField = "zt_No" DataNavigateUrlFormatString = "xiugai.aspx? zt_No = {0}" DataTextField = "zt_No" HeaderText = "Details" DataTextFormatString = "Modify"> </ asp: HyperLinkColumn>
<asp: ButtonColumn Text = "Delete" CommandName = "Delete"> </ asp: ButtonColumn>
</ Columns>

The delete event in the .cs file is defined as follows:
protected void datagrid1_DeleteCommand (object source, DataGridCommandEventArgs e)
    {
        try
        {
            OleDbConnection cn = new OleDbConnection ();
            cn.ConnectionString = @ "Provider = Microsoft.Jet.OLEDB.4.0; Data Source =" + Server.MapPath ("zt.mdb");
            string str = "delete zt where zt_No = @ zt";
            OleDbCommand cm = new OleDbCommand (str, cn);
            cm.Parameters.Add ("@ zt", OleDbType.Integer);
            cm.Parameters ["@ zt"]. Value = e.Item.Cells [0] .Text.ToString ();
            cn.Open ();

            cm.ExecuteNonQuery ();
            cn.Close ();
            Bind ();
        }
        catch (Exception ee)
        {Response.Write (ee.ToString ());}
    }

After testing, the data must be deleted unsuccessfully, and there are no errors, and I feel that after the point is deleted, the event processing is not driven at all! Online experts solve problems! Thanks
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-8-26 13:00:01
| Show all posts
How do you bind data in Page_Load?
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-26 13:15:01
| Show all posts
int id=Convert.ToInt32(e.Item.Cells[0].Text);
Delete him in the same way as other deletions
Finished binding the data source
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-26 14:00:01
| Show all posts
Debug the breakpoint to see if the datagrid1_DeleteCommand event is executed. If not executed, then in private void InitializeComponent()
{
//Join
this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridItemEventHandler(this.datagrid1_DeleteCommand);
}
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-26 16:00:01
| Show all posts
Put your <asp:BoundColumn DataField="zt_No" SortExpression="0" ReadOnly="True" HeaderText="Number"> </asp:BoundColumn>
Turn it into a template to see
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list