Guide for using web DataGrid in ASP. NET-go

Source: Internet
Author: User

DataGrid/datalist is very important in ASP. NET. Most of the two controls are used to display table data.

I. Methods

1. databind
This is a simple and most commonly used method. Bind data. Note only one thing: After this method is executed, the DataGrid (because the DataGrid and datalist are very similar, the following introduction is not far from datalist although it is for the DataGrid) all the controls that display the bound data will display the data in datasource, and other controls will also be initialized. the design status in Aspx.

Ii. Attributes

1. datasource
Where databind exists, datasource exists. If databind is executed without a datasource specified, the DataGrid will display nothing.
Datasource is generally dataset, able, or dataview. Of course, you can also bind datareader or other classes that implement ienumerable.

2. datakeyfield, datakeys
After you locate a row in the DataGrid, you must know the position of the row in the data table. There are at least five ways to do this. Setting datakeyfield in the DataGrid is one of these methods.
Datakeyfield is generally set to the unique field of the data table (otherwise it doesn't make sense). datakey can be used to obtain the value of the corresponding keyword segment for this row.
Datakeys is a set of datakeys that read the datakey of the corresponding row through the row index.

3. edititemindex, selectedindex, currentpageindex, selecteditem
These attributes are well understood. You can see what the name means. Note that you need to re-execute the databind method after setting edititemindex or currentpageindex (of course, as mentioned above, you also need to set datasource ).

4. Columns
There is nothing to explain. columns is columns, a set of columns. You can set column attributes, including visible, headertext, footertext, and sortexpression.
Note: automatically generated columns are not included in columns. Only the declared columns andCodeTo be included in the column.

5. Items
As the saying goes, the last thing is the most important. We will introduce items as the last attribute for this reason.
Items is a collection of datagriditem. You can traverse the datagriditem of the data displayed in the current DataGrid.
5.1. datagriditem
Each datagriditem is a row displayed in the DataGrid, including:
Header DataGrid Control title
Items in the item DataGrid Control
Alternate items in the alternatingitem DataGrid Control
Select items in the selecteditem DataGrid Control (set by selectedindex and read by selecteditem attribute or items [selectedindex)
Edit items in the edititem DataGrid Control (set by edititemindex and read by items [edititemindex)
Delimiter between items in the separator DataGrid Control
Footer of the footer DataGrid Control
Page selection section of the pager DataGrid Control
Note that the items attribute of the DataGrid does not contain the three types of datagriditem: Header, footer, and pager.
5.1.1. datagriditem attributes
Itemindex -- get the index of the row in items
Itemtype -- return the type of the row, that is, the header, item,..., pager listed above.
Cells -- returns all tablecells contained in a row (whether it is declared or automatically generated, whether visible or hidden, you can read the text and controls displayed in the cell.
Note: Only the boundcolumn column and automatically generated column can read the displayed text through the tablecell. Text attribute. Hyperlinkcolumn, buttoncolumn, and editcommandcolumn all need to convert the target control to the corresponding control.
For example:
Assume that the first column of the DataGrid is declared as follows:
<Asp: hyperlinkcolumn datatextfield = "au_id" headertext = "au_id" datanavigateurlfield = "au_id" datanavigateurlformatstring = "Edit. aspx? Id = {0} "> </ASP: hyperlinkcolumn>
You can use:
// Items [0] indicates the first row, cells [0] indicates the first column, and controls [0] indicates the first control in the cell (only this control can be used)
Hyperlink link = (Hyperlink) datagrid1.items [0]. cells [0]. controls [0]);
Response. Write (link. Text );
For the template column (templatecolumn), you can also use datagrid1.items. cells [J]. controls [N] is obtained, and then converted to the original control type before the operation, but there is a better way to use findcontrol to find the control.
Findcontrol is the method of system. Web. UI. control. You can find the child Control Based on the Child Control ID.
For example:
Assume that the declaration of a column in the DataGrid is as follows:
<Asp: templatecolumn>
<Itemtemplate>
<Asp: textbox runat = "server" id = "txtid" text = <% # databinder. eval (container. dataitem, "au_id") %>
</ASP: textbox>
</Itemtemplate>
</ASP: templatecolumn>
Read method:
Textbox TXT = (textbox) datagrid1.items [1]. findcontrol ("txtid ");
Response. Write (txt. Text );
Note: No cell exists in datalist.

Iii. Events

1. itemcommand, cancelcommand, deletecommand, editcommand, updatecommand
That is, in the DataGrid, click the button and linkbutton to execute the event. The executed event depends on the commandname of the button. In fact, the most important one is itemcommand, and the last four are only a small part of itemcommand,
For example, if the commandname of a button is "cancel", the itemcommand event is executed first and then the cancelcommand event is returned.

2. pageindexchanged
If your DataGrid is paginated, this event is triggered when you click 1, 2, 3, or <,> On the DataGrid.
In this event, you can use E. newpageindex to read the page to be changed, assign the value to the currentpageindex attribute of the DataGrid, and finally set datasource and execute databind.
Note: This event does not exist in datalist. If you need to paging in datalist, you can read data in a segment and then bind the data in the current segment to datalist.

3. itemdatabound, itemcreated
The first thing we should talk about is the time when these two events occurred.
Itemdatabound: If you execute the databind method, this event will be immediately triggered.
Itemcreated, if the page is accessed for the first time (page. ispostback = false). When databind is executed for the first time, the itemcreated event is first triggered. That is to say, after databind is executed, itemcreated is used to create header rows first, then, itemdatabound is used to bind the header row, itemcreated is used to create the first row, and itemdatabound is called to bind the first row, that is, itemcreated and itemdatabound are executed alternately.
When the page is returned, itemcreated is also executed.
Event, before page_load, but the itemdatabound event will not be executed at this time.
Therefore, if you want to dynamically add controls to the DataGrid, you need to add them in the itemcreated event instead of in the itemdatabound event.

 

Iv. code snippets

1. Display dual-layer table headers in the DataGrid
Assume that your DataGrid has three columns. Now you want to use the first two columns as "Category 1" and the third column as "Category 2". Now, you can add the following code to the itemdatabound event:
If (E. Item. itemtype = listitemtype. header)
{
E. Item. cells [0]. columnspan = 2;
E. item. cells [0]. TEXT = "Category 1 </TD> <TD> Category 2 </TD> </tr> <TD>" + E. item. cells [0]. text;
}
This method can be used to add any new rows.

2. Set the width of the edit box for binding columns or automatically generating Columns
Add the code to your itemdatabound event:
If (E. Item. itemtype = listitemtype. edititem)
{
For (INT I = 0; I <E. Item. cells. Count; I ++)
{
Textbox TXT = (textbox) E. Item. cells. controls [0];
TXT. width = unit. pixel (50 );
}
}

3. process the dropdownlist event in the DataGrid
Dropdownlist does not have the commandname attribute, so the itemcommand event cannot be used, but you can try it like this:
Dropdownlist control added to the template column of the DataGrid
<Asp: dropdownlist runat = "server" id = "DDL" autopostback = "true" onselectedindexchanged = "ddl_selectedindexchanged"/>
Then you add a function in. aspx. CS.
Protected void ddl_selectedindexchanged (Object sender, system. eventargs e) // It must be declared as protected or public, and cannot be private.
{
// Other code can be added here
}

3.1. How can I obtain the values of other cells in the preceding event?
As we know, the DataGrid is a table-structured control. The DataGrid contains the datagriditem, and each datagriditem contains the tablecell. Then, in a control of the tablecell, use the parent of the control to obtain tablecell. Then use the parent of tablecell to obtain the datagriditem.
protected void ddl_selectedindexchanged (Object sender, system. eventargs e) // It must be declared as protected or public, not private.
{< br> dropdownlist DDL = (dropdownlist) sender;
tablecell cell = (tablecell) DDL. parent;
maid item = (maid) cell. parent;
response. write (item. cells [0]. text);
}

4. How to obtain the control in the header, footer, and pager
Method 1: In itemcreated or itemdatabound, the specific code is not written more
Method 2: traverse all items in the DataGrid (note that it is not to traverse the items under datagrid1.items)
foreach (datagriditem item in datagrid1.controls [0]. controls)
{< br> If (item. itemtype = listitemtype. header)
{< br> // use item. findcontrol: Find the corresponding control
}< BR >}< br> you may notice that there is a datagrid1.controls [0]. controls, which indicates that there is a subcontrol under the datagrid1. This subcontrol is of the datagridtable type, and below is the set of datagriditems
In datalist, the following subcontrol is directly a datalistitem, no table:
foreach (datalistitem item in datalist1.controls)
{< br> //....
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.