Bind column
Datafield = "datetime" Field name Headeertext = "Time" List head HeaderImagerURL = ""> " Displayed on the picture in the column page, this image replaces Headertext text asp: boundcolumn> If "automatically creates columns" is true, the binding list is displayed, and then the auto column is displayed, and the automatically generated column will not be added to the columns collection. Super link column Text = "Text" // All columns show the same text, at this time, DataTextField is preferred DataTextField = "Code" // Binding field name DATATEXTFORMATSTRING = "" " // From the display format of the DataTextField NavigateURL = "URL" // All columns use the same url datanavigateURLFIELD = "codeID" // URL field variable, that is, the transmitted variable, sometimes and DataTextField DataNaviGateURLFORMATSTRING = "Webform2.aspx? Code = {0}" URL format string, string passed in GET mode Target = "_ blank"> / / Open the link to open the location or method asp: hyperlinkcolumn> Button column Ordinary button TEXT = "All Columns Unified Button Name" // All column unified buttons DataTextField = "Hold Name" // Bind field CommandName = "btn"> // Headertext = "Operation"> // List head asp: buttoncolumn> Select button Text = "Select" DATATEXTFIELD = "Shares Name" CommandName = "SELECT"> asp: buttoncolumn> Edit button Buttype = "LinkButton" UpdateText = "Update" CancelText = "Cancel" EditText = "Edit"> asp: editcommandcolumn> Delete button TEXT = "Delete" Buttype = "pushbutton" CommandName = "delete"> asp: buttoncolumn> Columns> CommandName is set in the DataGrid1_ItemCommand () event Get which button in the same row is clicked: string s = e.commandname; / / The default is LinkButton, must also be LinkButton Click the button First respond to the DataGrid1_ItemCommand event private void DataGrid1_itemCommand (Object Source, System.Web.ui.WebControls.DataGridCommandeventArgs E) { TableRow Tr = E.Item; // Get the current line of the operation, save the control string code = tr.cells [1] .Text; // Cluscreen is then given the text string time = tr.cells [2] .TEXT; TableCell Cell1 = E.Item.cells [1]; // This can also get the value of the cell, deposit the control Server.Transfer ("Webform2.aspx? Code =" Code "& OR TIME =" TIME); } Then the different button responds to different events: edit Private void DataGrid1_editCommand (Object Source, System.Web.ui.WebControls.DataGridCommandeventArgs E) { This.dataGrid1.edititeMindex = E.Item.itemindex; THIS.DATASHOW (); } Update Private void DataGrid1_updateCommand (Object Source, System.Web.ui.WebControls.DataGridCommandeventArgs E) { // Get the value of the primary key INT i = (int) this.dataGrid1.datakeys [E.Item.ItemIndex]; Or String II = (String) this.dataGrid1.DataKeys [E.Item.ItemIndex]; According to the primary key, update the record corresponding to the primary key with the cell data. // Write an UPDATE statement } cancel Private void DataGrid1_cancelcommand (Object Source, System.Web.ui.WebControls.DataGridCommandEventArgs E) { THIS.DATAGRID1.EDITITEMINDEX = -1; THIS.DATASHOW (); } Note: You can use the primary key binding to read only; delete / / Should first set the DATAKEYFIELD attribute as the main key column Private void DataGrid1_deleteCommand (Object Source, System.Web.ui.WebControls.DataGridCommandeventargs E) { / / Get the value of the primary key column INT i = (int) this.dataGrid1.datakeys [E.Item.Item.Index]; Or String II = (String) this.dataGrid1.DataKeys [E.Item.ItemIndex]; // write to the delete statement, } When sending between the server, select different items in the data list control, SELECTEDIDEXCHANGED event You can also get primary key values by int I2 = (int) this.DataGrid1.datakeys [this.dataGrid1.selected "; DataKeyField is a field, all of his key value content is filled in the DataKeys collection, and the primary key value of a record via DataKeys [] Sort Specify the default sort: Select "Automatically create columns" TRUE; In the Behavior section, select the Allow Sort by. In the SortCommand event, the view is re-sorted by E.Sortexpression. (Disadvantages: Every column has a "link" button,) Specify custom sort: Select "Automatically create columns" false; Set SortExpression in the column that needs to be sorted Note: Columns without sorting expressions will not trigger SortCommand events, so set sort expressions first Private void DataGrid1_sortcommand (Object Source, System.Web.ui.WebControls.DataGridsortCommandeventArgs E) { String SQL = "Server = 127.0.0.1; Database = LTP; user ID = sa; password ="; SqlConnection mycon = new SqlConnection (SQL); String selsql = "SELECT * FROM DATA"; SqlDataAdapter Da = New SqlDataAdapter (Selsql, MyCon); DataSet DS = New Dataset (); Da.fill (DS, "DATA"); DataView DV = New DataView (ds.tables ["data"]); Dv.Sort = E.Sortexpression.toString (); // Settings Sorting Keywords this.DataGrid1.datasource = DV; // Re-bound THIS.DATAGRID1.DATABIND (); } When the field data is displayed by the template, sort can be performed in the The Commandargument of the button is set to sort expressions (for SortCommand events) Button's CommandName = "sort", be sure to lowercase Mold column You can decide which controls (one or more) you want to display in the column, and which fields are to be bound. Add a mandatory version of the button will reverse its Click event to DataGrid1_itemmnd Event, but to judge CommandName Edit the template / in the template / mover, dragging the control. Note If you call the parent control ( DataList, REPEATER or DataGrid control) DataBind method, The ItemCommand event will not happen because the contents of the parent control have been reset. Therefore, you usually do not need to call each round trip. DataBind method (ie no need to check back when the page is initialized). Template Column font> // Header HeadeRTemplate> asp: imagebutton> Itemtemplate> asp: templateColumn> Get the value of the control in the temple column ( DataGrid1_itemmnd In an event) DropDownList ListCtr = (DropDownList) E.Item.FindControl ("DropDownList2"); String s = listctr.selectedValue; Checkbox Box = (Checkbox) E.Item.FindControl ("CheckBox1"); BOOL B = Box.Checked;