DataGrid's events, ItemCreated and ItemDatabase are particularly important and often used, where the itemcreated event triggers when DataGrid creates a table row, and ItemDatabase is bound to the corresponding row, the former is triggered in the data binding. You can use a dynamic control in the DataGrid, such as adding a button in the DataGrid's title row, and the itemDatabase is triggered, and the data has been bound to the corresponding line, then you can Get the data of the current line, and perform the corresponding operation based on this, such as the Book value of the binding to the "Sex" field with String, selected status, etc., examples, examples, examples The code is as follows, saved as an ASPX file and placed in the virtual directory:
<% @ Page language = "c #" Debug = "true"%> <% @ import namespace = "system"%> <% @ Import namespace = "system.data"%>
Void Page_Load (Object Src, Eventargs E) {if (! page.ispostback) {dataable table = new data (); table.columns.add (New Datacolumn ("Number, TypeOf (int)); Table.Columns. Add ("Name", TypeOf (String)))); // Sex Column, with BOOL Value, male True, female False Table.Columns.Add (New Datacolumn ("Gender", Typeof (Bool) )); Table.columns.add (New Datacolumn ("Forbidden Login", TypeOf (Bool)); DATAROW ROW; Random Rd = New Random (); for (INT i = 0; i <10; i ) {ROW = Table.NewRow (); Row [0] = i 1; Row [1] = "User" (i 1) .tostring (); row [2] = (Rd.Next (0,80)% 2 == 0); // use the random number to generate gender, to show the principle of fairness :) Row [3] = (i% 2 == 0); Table.Rows.add (row);} myDataGrid.DataSource = Table MyDataGrid.databind ();}}
void MyDataGrid_ItemCreated (object src, DataGridItemEventArgs e) {if (e.Item.ItemType == ListItemType.Header) {Button btn = new Button (); btn.Text = e.Item.Cells [3] .Text; btn.Click = New EventHandler (BTN_Click); E.Item.cells [3] .controls.clear (); E.Item.cells [3] .controls.add (btn);}}
void MyDataGrid_ItemDataBound (object src, DataGridItemEventArgs e) {if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {DataRowView rv = (DataRowView) e.Item.DataItem; // Replace the BOOL value of the gender column to the corresponding text "Men" or "Female" IF ((Bool) RV ["Sex" == True) E.Item.cells [2] .Text = "Men"; Elsee. Item.cells [2] .Text = "Female"; // Set Checkbox's Select Status CheckBox CKB = E.Item.FindControl ("CKB_Denylogin") as checkbox; if ("ckb_denylogin") as checkbox; if ("ckb_denylogin") as checkbox; if ("ckb_denylogin") ) {ckb.checked = (bool) RV ["" forbidden login "];}}}
Void BTN_Click (Object SRC, Eventargs E) {String DeniedUsers = ""; // Save the user number of "prohibited login" user number CheckBox CKB; for (int i = 0; I