DataGridDataList, will you use it?

xiaoxiao2021-03-06  92

By icyer from 9cbs I wanted to write something about DataGrid / Datalist, but I have always been deep, on the one hand, I haven't worried, on the other hand, I have no pen, one or then drag, the idea of ​​the beginning More than one year. DataGrid / Datalist's importance in ASP.NET, you don't need to have to emphasize, wherever the Table type data, most of them will use these two controls (of course, if anyone writes asp.net like ASP, then I There is no way), so everyone may have their own comprehension, this article, it is a bail, make a paving for everyone. 1. Method 1, Database is simple and most commonly used. Binding data. There is only one thing to pay attention to: After the method is executed, DataGrid (Due to the very similar DataGrid and DataList, the following introduction is a control for DataGrid, but the DataList also is not far from the control of all display binding data, will show Dataource in DataSource, the remaining controls will also initialize the status of the design in .ASPX. Second, attribute 1, DataSource has Database, there should be DataSource. If DataBind is not specified, DataGrid will not display anything. DataSource is generally DataSet, DataTable or DataView. Of course, you can also bind DataReader or other classes that implement the Ienumerable. 2, DatakeyField, Datakeys When you locate a line in DataGrid, you must know that this line is in the location of the data, at least five methods can do this, setting the DataGrid's DatakeyField is one of these methods. DataKeyField is typically set to the UNIQUE field of the data table (otherwise it is meaningless), and the value of the key field corresponding to this line can be obtained by Datakey. DataKeys is a collection of Datakey, reading the corresponding line of Datakey through the index of the row. 3, edititemindex, selectedindex, currentpageindex, selecteditem these properties are very understanding, see what the name knows what it means, it is necessary to pay attention to it is necessary to re-execute the DataBindex method after setting EditIndex or CurrentPageIndex (of course, mentioned earlier, need Set DataSource). 4, columns is not well explained, columns is Columns, columns, which can set the properties of the columns, including Visible, Headertext, Footertext, Sortexpression, and so on. Serious attention: Automatically generated columns are not included in columns. Only columns displayed in .aspx and columns added in the code will be included there. 5, Items As the saying goes, the last is the most important, introduces it as the last attribute, formally based on such a reason. Items is a collection of DataGridItem that can spread DataGridItem that displays data in the current DataGrid.

5.1, each DataGridItem DataGridItem DataGrid is displayed in a row, which comprises: alternately selected items in the control entry SelectedItem DataGrid control header section Item Control Header DataGrid DataGrid control in terms AlternatingItem DataGrid (set by the SelectedIndex, SelectedItem by Attribute or items [selectedindex] to read) EDITITEM DATAGRID controls in editing status (set by EditIndex settings, read by items [edititemindex]) Separator DataGrid control Sneers Footer DataGrid control footnotes part of the footnotes section Page Selection Section of the DataGrid Control Note that there is no Header, Footer, and Pager for the three types of DataGridItem. 5.1.1, DataGridItem's properties ItemIndex - get the index itemtype in Items - Return line, which is the header, item, ..., pagercels listed above - Return to all Tablecells contained in the row (regardless of Is it displayed or automatically generated, whether it is seen or hidden? Read the displayed text through the Tablecell.Text property. HyperLinkColumn, ButtonColumn, EditCommandColumn requires converting the target control into a corresponding control. For example: Assuming that the first column is declared as follows DataGrid Read You can use: // items [0] to represent the first line, Cells [0] represents the first column, controls [0] represents the first control in Cell (only this control can be used) HyperLink Link = HyperLink) DataGrid1.items [0] .Cells [0] .controls [0]); Response.write (Link.Text); As for templateColumn, of course, can also pass DataGrid1.Items [i] .cells [J ] .Controls [n] to get, then convert to the original control type and then operate, but there is a better way to find controls with FindControl.

FindControl is the method of System.Web.ui.Control, which can find sub-controls, such as sub-control ID, such as: Suppose a column declaration of DataGrid is as follows Read method: TextBox txt = (TextBox) DataGrid1.items [1] .FindControl ("txtid"); response.write (txt.text); Note: DataList is no Cell 3, event 1, itemcommand, ca Zancommand, deleteCommand, editcommand, UpdateCommand is DataGrid, Click on the event after Button, LinkButton, and the executed event depends on the button's commandName. In fact, one of the most important one is ItemCommand, while four are just a small part of ItemCommand, such as a button's commandName is "Cancel". When returns, first execute the itemCommand event, and then the CancelCommand event. 2, PageIndexchange, if your DataGrid is paging, then when you click on 1, 2, 3 or <,> when you click on the Pager, you will inspire this event. In this event, you can use E.NewpageIndex to read the page you want to change, then assign the value to the CurrentPageIndex property of DataGrid, and finally don't forget, set DataSource, but also DATABIND. Note: There is no event in DataList, if you need to paginize in DataList, you can read the data from a segment, then bind the data of the current segment to the DataList. 3, itemdatabase, iTemcreated, first is to say, the time of occurrence of these two events. ItemDatabase, as long as the DATABIND method is executed, this event will immediately. Itemcreated, if the page is the first visit (page.ispostback = false), when executing Database for the first time, ItemCreated event first, that is, after DATABIND, first use itemcreated to create Header OK, then use itemDatabase to bind the Header line, then use itemcreated to create the first line, then call ItemDatabase to bind the first line, that is, itemcreated and itemdatabase are alternately executed. When the page returns, the itemcreated event will also be executed, but before Page_Load, it will not perform the itemDatabase. So, if you want to add any controls in DataGrid, you need to be in the itemcreated event, not in the ItemDatabase event.

Fourth, Data Dischart 1, DataGrid Shows Double Skull Header Suppose Your DataGrid has three columns, now I want to use the first two columns as "big category 1", the third column is "big category 2", now you can in the ItemDatabase event Add the following code: if (E.Item.itemType == ListiteMTYPE.HEADER) {E.Item.cells [0] .COLUMNSPAN = 2; E.Item.cells [0] .text = "Class 1 Class 2 " E.Item.cells [0] .Text;} It can be added to any new row. 2. Set the edit box width of the binding column or automatically generated column, please add the code in your ItemDatabase: if (E.Item.itemType == ListItemType.editItem) {for (int i = 0; i Then you join a function protected void ddl_selected in .aspx.cs, you must declare it into protected or public, and cannot be private. {// You can add another code} 3.1, how to get the value of other Cells in the Bank in the above event? We know that DataGrid is completely a Table structure control, which contains DataGridItem, and each DataGridItem includes Tablecell, then we can use the control's Parent to get Tablecell in a control of TableCell, and use Tablecell's Parent, You can get DataGridItem. Protected Void DDL_SELECTEDINDEXCHANGED (Object Sender, System.EventArgs E) / / must be declared as protected or public, can't be private.

{DropDownList DDL = (DropDownList) Sender; TableCell Cell = (Tablecell) DDL.Parent; DataGridItem Item = (DataGridItem) Cell.Parent; response.write (item.cells [0] .text);} 4, how to get header, Footer, Pager control method 1: In itemcreated or itemdatabase, the specific code is not written in more: Traversing all Items of DataGrid (note, it is not traversed under DataGrid1.Items) Foreach (DataGridItem Item ITAGRID1.CONTROLS [0] .controls) {if (item.ItemType == ListiteMTYPE.HEADER) {// Find the corresponding control with item.findControl}} You may notice that there is a DataGrid1.controls [0] .controls, this Indicates that under DataGrid1, there is a child control, which is a DataGridTable type. He is the DataGridItem collection in DataList, which is DataListItem directly, not Table: Foreach (DatalistItem Item in DataList1.controls) { // ....} Ok, I will write this here. If you are not perfect, please add, if you have a wrong place, please refer to you. I would like to give this article, dedicated to all friends who love the technology, support forums (Icyr, March 19, 2003) Posted on Saturday, February 28, 2004 2:26 PM

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

New Post(0)