There are a lot of DataGrid users, of which data binding and paging are an old problem. Here I just talk about my personal experience. After all, I am a newbie.
There are many ways to bind a DataGrid. You can use dataview, datareader, and dataset to bind them. You can also use custom controls to bind them. In fact, the corresponding data display is used. There are also many types of paging: 1. Using SQL statements, select top 10 from XXX Where xxx, followed by not in (select top 10 from XXX Where xxx ). 2. Read data once and then useProgramControl pages.
The following is a simple paging and binding process:
Create a DataGrid first 1 < ASP: DataGrid allowpaging = " True " Allowsorting = " True " Autogeneratecolumns = " False " Width = " 100% " Onpageindexchanged = " Changepage "
2 Footerstyle - Backcolor = " # 0099ff " ID = " Datagrid1 " Itemstyle - Backcolor = " #006699 " Pagerstyle - Nextpagetext = " Next Page " Pagerstyle - Prevpagetext = " Previous Page " Pagesize = " 20 " Runat = " Server " >
3 < Columns >
4 < ASP: boundcolumn headertext = " User ID " Headerstyle - Width = " 100 " Itemstyle - Width = " 50 " Datafield = " _ Userid " />
5 < ASP: boundcolumn headertext = " User Name " Headerstyle - Width = " 100 " Itemstyle - Width = " 100 " Datafield = " ID " />
6 < ASP: boundcolumn headertext = " Email " Headerstyle - Width = " 150 " Itemstyle - Width = " 150 " Datafield = " Email " />
7 < ASP: boundcolumn headertext = " Type " Headerstyle - Width = " 50 " Itemstyle - Width = " 50 " Datafield = " Type " />
8 </ Columns >
9 </ ASP: DataGrid >
In this example, allowpaging allows automatic paging, allowsorting allows automatic sorting, and autogeneratecolumns allows automatic generation of columns (you must set the column to false if you want to define the column yourself), followed by pagination events, pagerstyle-mode is the type displayed below the page. The default type is the previous page, the next page, and numericpages. The type displayed is 12345. The columns below are the columns to be displayed, and datafield is the field name to be bound.
Then write page_load.CodeTo be bound at the beginning, it is to query the database to obtain data.
1 String SQL = " Select _ userid, ID, type, email from user where city! = 'Null' order by _ userid DESC " ;
2 String Connstr = " Uid = sa; Password = 123; database = KKK; server = (local) " ;
3 Sqlconnection Conn = New Sqlconnection (connstr );
4 Sqldataadapter comm = New Sqldataadapter (SQL, Conn );
5 Dataset isset = New Dataset ();
6 Comm. Fill (isset, " User " );
7 Dataview isview = New Dataview (isset. Tables [ " User " ]);
8 Datagrid1.datasource = Isview;
9 Datagrid1.databind ();
Here we use dataview to bind the data. First, we define the connection, then read the data through sqldataadapter, then fill the data in the User table in the dataset, and send the content in the dataset table to dataview, then bind the DataGrid. In fact, this is very simple. In the DataGrid and XML read data that I sent earlier, there is a method to read the XML data binding. You can also set sort to sort the data.
Next, we will define the paging events in the DataGrid.1 Datagrid1.currentpageindex=E. newpageindex;
2 Binding ();
The binding () is the binding code above. Here, you can write the binding code as a function to call it.
This completes simple data reading, binding, and paging.
In the process of use, we sometimes need to process and display the queried data, for example, to query the User Type 1, but we need to see whether it is a common user or a special user, here we need to use the custom control for time. 1 <% @ Register tagprefix = " User " Tagname = " Money " SRC = " Xxx. ascx " %>
2
3 < ASP: templatecolumn headertext = " Recharge quantity " Headerstyle - Width = " 70 " Itemstyle - Width = " 200 " >
4 < Itemtemplate > < User: Money ID = " Check1 " Runat = " Server " UID = <% # Databinder. eval (container. dataitem, " ID " ) %>/>
5 </ Itemtemplate >
6 </ ASP: templatecolumn >
Here we will first write XXX. ascx, then register the tag in the webpage (the first line is), and then use it in the DataGrid. Here we will use ASP: templatecolumn to bind it. <% # Databinder. eval (container. dataitem, "ID") %> is the bound content, and the previous section references the custom control. By the way, the binding method in columns is demonstrated in the previous section. Some are the first datafiled method, and some are the ASP: templatecolumn, however, <% # databinder. eval (container. dataitem, "ID") %>
The above are some simple ways to use the DataGrid for communication.