DataGrid's column operation personal summary

xiaoxiao2021-03-06  50

Bind column

Datafield = "datetime"

Field name Headeertext = "Time"

List head

HeaderImagerURL = ""> "

Displayed on the picture in the column page, this image replaces Headertext text

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

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

Select button

Text = "Select" DATATEXTFIELD = "Shares Name"

CommandName = "SELECT">

Edit button

Buttype = "LinkButton"

UpdateText = "Update"

CancelText = "Cancel"

EditText = "Edit">

Delete button

TEXT = "Delete"

Buttype = "pushbutton"

CommandName = "delete">

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 add-console of the template column.

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

// Header

// This line is the displayed control or text when selecting an editing button

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;

转载请注明原文地址:https://www.9cbs.com/read-115863.html

New Post(0)