Bind Column <Asp: boundcolumn
Datafield = "datetime" field name
Headertext = "time" list header headerimagerurl = ""> the image displayed in the column header replaces the text of headertext.
</ASP: boundcolumn> If the value of "automatically create columns" is true, the bound columns are displayed first, and then the automatically generated columns are not added to the columns set.
Hyperlink Column
<Asp: hyperlinkcolumn
TEXT = "text" // The same text is displayed in each column. In this case, datatextfield takes precedence.
Datatextfield =" Code "// Bound field name
Datatextformatstring = "" // customize the display format of datatextfield
Navigateurl = "url" // all columns use the same URL
Datanavigateurlfield = "codeid" // URL field variable, that is, the passed variable value, sometimes the same as datatextfield
Datanavigateurlformatstring = "webform2.aspx? Code = {0} "url Format String, string passed in get Mode
Target = "_ blank"> // location or method of opening a link
</ASP: hyperlinkcolumn>
Button Column
<Columns>
Normal button
<Asp: buttoncolumn
TEXT = "unified button names for all columns" // unified button names for all columns
Datatextfield = "shareholding name" // binding field
Commandname = "BTN"> // headertext = ""> // list Header
</ASP: buttoncolumn>
Select button
<Asp: buttoncolumn
TEXT = "select"
Datatextfield = "shareholding name"
Commandname = "select">
</ASP: buttoncolumn>
Edit button
<Asp: editcommandcolumn
Buttontype = "linkbutton"
Updatetext = "Update"
Canceltext = "cancel"
Edittext = "edit">
</ASP: editcommandcolumn>
Delete button
<Asp: buttoncolumn
TEXT = "delete"
Buttontype = "Pushbutton"
Commandname = "delete">
</ASP: buttoncolumn>
</Columns>
Commandname is used to obtain which button in the same row is clicked in the datagrid1_itemcommand () Event: String S = E. commandname; // It is a linkbutton by default and must also be a linkbutton
Click the button to 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 row of the operation and save it to the control.
String code = tr. cells [1]. Text; // The cluster control then obtains the text of the cell.
String time = tr. cells [2]. text; tablecell cell1 = E. item. cells [1]; // you can obtain the cell value and store it to the control server. transfer ("webform2.aspx? Code = "+ code +" & or time = "+ time );
} Then different buttons respond 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)
{// Obtain the value of the primary key column
Int I = (INT) This. datagrid1.datakeys [E. item. itemindex]; or string II = (string) This. datagrid1.datakeys [E. item. itemindex]; update the record corresponding to the primary key with the data of cells based on the primary key // write the update statement
} Cancel private void
Datagrid1_cancelcommand (Object source, system. Web. UI. webcontrols. datagridcommandeventargs E)
{
This. Maid =-1;
This. datashow ();
} Note: You can set the Primary Key Binding column to read-only. Delete // you must first set the datakeyfield attribute to the private void of the primary key column.
Datagrid1_deletecommand (Object source, system. Web. UI. webcontrols. datagridcommandeventargs E)
{
// Obtain the value of the primary key column
Int I = (INT) This. datagrid1.datakeys [E. item. itemindex]; or string II = (string) This. datagrid1.datakeys [E. item. itemindex]; // write the delete statement,
} When different items are selected in the data list control between sending requests to the server
Selectedindexchanged You can also use int I2 = (INT) This. datagrid1.datakeys [this. datagrid1.selectedindex]; The obtained primary key value datakeyfield is a field. All its key values are filled in the datakeys set, and the primary key value of a record is obtained through datakeys [].
Sort
Specify default sorting: Select "auto create column" true; in the "behavior" section, select the "allow sorting" box. In the sortcommand event, re-sort and bind views by E. sortexpression. (Disadvantage: each column has a "Link" button.) specify custom sorting: Select "auto create column" and set "sortexpression" in the column to be sorted.
Note: columns without sorting expressions do not trigger sortcommand events. Therefore, first set the sorting expression 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 (); // set the sorting keyword this. datagrid1.datasource = DV; // rebind
This. datagrid1.databind ();
} When field data is displayed through the template column, you can use the
Template Column You can decide which controls (one or more) to display in the column and which fields to bind to these controls. The button to add the template column will reverse the click event to the datagrid1_itemcommand event. However, you must determine the commandname to edit the template \ and drag the control directly into it.
Note: If you call the parent Control (
Datalist ,
Repeater Or
DataGrid Controls)
Databind Method,
Itemcommand The event does not occur because the content of the parent control has been reset. Therefore, you do not need to call
Databind Method (no need to check and send back the page during initialization ). <Asp: templatecolumn> <Font face = ""> template column </font> // Header
</Headertemplate> <itemtemplate>
<Asp: imagebutton id = "imagebutton1" runat = "server" imageurl = "D: \ IMG \ image \ button_add.gif"> </ASP: imagebutton>
</Itemtemplate>
<Edititemtemplate> // specifies the control or text displayed when the edit button is selected.
</Edititemtemplate>
</ASP: templatecolumn> get the value of the control in the template column (in the datagridshortitemcommand event) dropdownlist listctr = (dropdownlist) E. Item. findcontrol ("dropdownlist2 ");
String S = listctr. selectedvalue; checkbox box = (checkbox) E. Item. findcontrol ("checkbox1 ");
Bool B = Box. Checked; convenient to use the template Column
<Asp: templatecolumn>
<Itemtemplate>
<A href = "javascript: window. Open ('ddd. aspx? Id = <% # databinder. eval (container, "dataitem, ID") %> & xxx = xxx '); "> link </a>
</Itemtemplate>
</ASP: templatecolumn> you can add a linkbutton column to the Attribute Editor and write it in the datagride_selecteditemchanged file.
Response. Write ("javascript: window. Open ('A. aspx? Id = '"+ databinder. eval (container, "dataitem, ID") + ", 'mywin', 'left = 20, Top = 20, width = 500, Height = 500, toolbar = 1, resizable = 0 ');")