I have never written some things about DataGrid / Datalist, but I have always been in the on-one, on the other hand, I will not be in the pen, and I will be dragged, and the idea of just starting is a 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?, By TableCell, you can read the text displayed in the cell, the contained controls are serious: only boundcolumn columns and automatic generation columns can be 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
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
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 TD >
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 3, dealing with DROPDOWNLIST's DROPDOWNLIST DROPDOWNLIST does not have a CommandName property, so you can't use itemcommand events, but you can try this: DropDownList control added in the DataGrid template column 3.1 How to get the value of the other CELL 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 (attention, not traversing the item under DataGrid1.items) Foreach ( DataGridItem Item in DataGrid1.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 means 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) {//....}//from msdn by lyhSelecting rows by Clicking AnywhereThe default model for selecting rows in the grid is for you to add a Select button (actually, a LinkButton control) whose CommandName property is Set to "select." When the Button is Clicked, The DataGrid Control Receives The SELECT Command and Automatic Displays The Row in SELECTED MODE. Not everyone likes having an explicit Select button, and a common question is how to implement the feature where users can click anywhere in a grid row to select it. The solution is to perform a kind of sleight-of-hand in the grid. You add the Select LinkButton control as normal. Users can still use it, or you can hide it. In either event, you then inject some client script into the page that effectively duplicates the functionality of the Select button for the row as a whole. The example below shows how. In the grid's ItemDataBound handler, first make sure that you are not in the header, footer, or pager. Then get a reference to the Select button, which in this instance is assumed to be the first control in the first cell. you then call a little-known method called GetPostBackClientHyperlink. This method returns the name of the postback call for the designated control. in other words, if you pass in a reference to a LinkButton control, it returns the name of the client function call that will perform the postback.Finally, you assign the client-side method to the item itself. When the grid renders, it renders as an HTML table. By assigning the method to the item, it is the equivalent of adding client- side code to each row ( Note One small disadvantage of this technique is that it adds somewhat to the stream rendered to the browser, and it adds information for each row to view state. 'Visual BasicPrivate Sub DataGrid1_ItemDataBound (ByVal sender As Object, _ ByVal e As System.Web. UI.WebControls.DataGridItemEventArgs) _ Handles DataGrid1.ItemDataBound Dim itemType As ListItemType = e.Item.ItemType If ((itemType = ListItemType.Pager) Or _ (itemType = ListItemType.Header) Or _ (itemType = ListItemType.Footer)) Then Return Else Dim Button as linkbutton = _ ctype (E.Item.cells (0) .controls (0), linkbutton) E.Item.attributes ("onclick") = _ page.getpostbackclienthyperlink (Button, ") End IFend Sub // C # private void DataGrid1_ItemDataBound (object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) {ListItemType itemType = e.Item.ItemType; if ((itemType == ListItemType.Pager) || (itemType == ListItemType. HEADER) || (itemtype == Listite MTYPE.FOOTER) {RETURN;} LinkButton Button = (linkButton) E.Item.cells [0] .controls [0]; E.Item.attributes ["onclick"] = page.getPostBackClienthyPerLink (Button, ""); } element) in the table. The grid's Item object does not directly support a way to assign client code to it, but you can do that by using its Attributes collection, which passes anything you assign to it THRough to the browser.