A long time ago, I wanted to write something about DataGrid/DataList. However, on the one hand, I have not learned much about myself, and on the other hand, I have never been able to get started, it has been more than a year since the beginning.
DataGrid/DataList in ASP. NET, I do not need to emphasize any more. Most of the two controls will be used for displaying Table data (of course, if anyone writes ASP as ASP. NET, so everyone may have their own insights. This article is a good example to help you lay the groundwork.
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.