Add a method of adding a delete button for the DataGrid control in C #

zhaozj2021-02-12  155

Introduction, we can implement the DataGrid with the delete button to delete the record, and there will be a confirmation process before deleting to avoid misoperation.

text

Add a method of adding a delete button for the DataGrid control in C #

Thunder: http://www.ai361.com/

When we use C # to develop, you often use the DataGrid control to display data in the database. For a record, modification and deletion is also what we often do, Raytheon likes to create data from itself. DataGrid's function, is not willing to use the setting method, so it also needs to be created manually when deleting records. You can have a variety of ways to implement this function, and the thunders will be a method: (The following code is a code snippet of Raytheon, please adjust according to the actual situation)

Step 1: Add a button column in the DataGrid.

Step 2: After completing the binding of the DataGrid, add a prompt information for the Delete button.

For (int i = 0; i

{

// Here. DataGrid1.items [i] .controls [6] .controls [0]; Please set it according to your DataGrid actual situation.

LinkButton lb = (linkbutton) this.dataGrid1.items [i] .controls [6] .controls [0];

Lb.attributes.add ("onclick", "JavaScript: if (! WINDOW.CONFIRM ('OK To delete this record?')) Return False;");

}

Step 3: Add a handler to the button

No private void DataGrid1_DeleteCommand (object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) {// get record int recid = int.Parse (this.DataGrid1.DataKeys [e.Item.ItemIndex] .ToString ()); If (....) // The conditions here are determined by Raytheon. DeleteOperate (Recid); // Delete function elseResponse.write ("