Henry Intention - Custom Patement of DataGrid in Web Form
Han Rui (05/31/2003)
ASP.NET brings us a lot of surprises, and the powerful web form control is naturally an important part of it. Among them, the most attention is of course DataGrid. The method of using an HTML mark syntax in ASP is so complicated in front of DataGrid data binding. But only the functionality of DataGrid itself is not reached. This article is to demonstrate how to improve our work efficiency and reduce the load pressure of the server.
DataGrid page display
When the query has a lot of data, when it is displayed in one screen, we can use the DataGrid's paging feature to let users view the data through the upper and lower pages. The setting method is simple: Click "Properties Builder" at the attribute of the DataGrid, and then perform the settings shown in Figure 1 in the "Paging" column, the number of lines of the page size determines several line data records.
Figure 1 Page Setup
Custom navigation bar
In the "Format" column in the properties page of Figure 1, we can set the appearance style of "page navigation". But many programming staff prefer to use their own navigation bar without using the default upper and lower page buttons or digital buttons (ie, the form of 1, 2, 3 ... page number). Then we can do this:
(1) If you want to keep the default navigation bar, don't look at this section. If you want to hide the default page navigation bar. To set the PageStyle's Visible property to false. Or in Figure 1 "Page Navigation", you do not check the "Display Navigation Button".
(2) When writing a custom navigation bar, it is mainly to set the page with the CurrentPageIndex property:
"First page":
DataGrid1.currentpageIndex = 0;
"Previous":
IF (DataGrid1.currentPageIndex> 0)
DataGrid1.currentpageIndex - = 1;
"Next page":
IF (DataGrid1.currentPageIndex <(DataGrid1.pageCount - 1))
DataGrid1.currentpageIndex = 1;
"the last page":
DataGrid1.currentpageIndex = (DataGrid1.PageCount - 1);
(3) Need not to note: Web Form is a stateless programming method, after we customize the code of the navigation bar button, you need to load the data source of DataGrid again. Otherwise, the effect of page switching will not be seen. That is, we need to add the code to reload the data source in the click Click event, such as we can write the loaded code in the following method setGridSource, and add the setGridSource () in the code of the button Click.
Private void setgridsource ()
{
SqlConnection MyConnection = New SqlConnection (YourownConnectionString);
String selectcommand = "select * from yourrtable";
Sqldataadapter mycommand = new sqldataadapter (selectcommand, myconnection);
DataSet DS = New DataSet ();
MyCommand.Fill (DS, "YourTable");
DataView DV = DS.TABLES ["YourTable"] .defaultview;
DataGrid1.datasource = DV; DataGrid1.databind ();
}
Or we can write the code in the setGridSource in page_load so that the browser can reload and bind the data source of the DataGrid when the browser can reset on page switching. It should be noted that the above code cannot be written in the first load determination if (! Ispostback) statement
(4) We generally need information such as the current page on the page. Unlike the DataGrid in WinForm, you need to get information related to data in WinForm, you can only get information from DataGrid, such as DataTable, and a certain value of a cell. Friends who have made dizziness between DataSet and DataGrid in WinForm, don't be afraid, in WebForm, we can get this information directly from DataGrid. The total number of pages is the pageCount property of DataGrid; the current page is the currentPageIndex attribute value 1, which is because CurrentPageIndex is counting from 0.
Custom paging
With the paging mode in the DataGrid property generator, we will feel convenient. However, with the method mentioned above, each page switches, all the SQL statements such as select * from yourtable will extract all of the data without any file-related filters. This greatly reduces the efficiency of our web program and also adds the burden on the server. The paging features provided by DataGrid have also become famous. We hope that the page can realize the ability to reduce the amount of data per download, you must write the code to control the amount of data per extracted.
To use custom paging, set the AlLowCustomPaging property to True. Then the value based on the value and the VirtualItemcount property is calculated to display the number of pages required for each of the DataGrid control.
PageSize indicates that the number of items displayed on the single page of the DataGrid control, the default value is 10.
VirtualItemCount is the total number. Then the number of pages is VirtualItemcount / PageSize
(1) Of course, you must first know how many data have a total of data, then we will do it, write in Page_Load:
IF (! ispostback)
{
StartIndex = 0; // StartIndex is the utility variable of int type
SqlConnection MyConnection = New SqlConnection (YourownConnectionString);
Sqlcommand mycommand = new sqlcommand ("SELECT Mycount = count (*) from table", myconnection;
MyConnection.open ();
SqlDataReader Dr = mycommand.executeRead (commandbehavior.singlerow);
IF (Dr.Read ())
DataGrid1.virtualItemcount = (int) DR ["mycount"];
Dr.close ();
MyConnection.Close ();
Setgridsource (StartIndex, "Previous"); // Take a look at what is different from now?
}
Note: Here is a knowledge point outside the research scope of this article. In the assignment code of the above DR, we use myCommand.executeReader (Commandbehavior.singlerow), and everyone is usually used by mycommand.executeReader (), but please remember Live, if you return a recordset, you can use the commandbehavior.singlerow to identify the efficiency of the application. This is because the MYCOMMAND execution is used in general, using the iRowSet interface. The iRow interface (if available) is used to perform the binding. Among them, please pay attention to the follow-up works of the author. (2) Now the main change is of course in the method of resourceing the data source setGridSource. We join the method to join two input parameters: the current page, the control (ie, the user clicks "Previous" is still "Next" and other buttons). The following code mainly implements the button navigation bar, there are two buttons "Previous" to "Next". The "First Page" and the "Last page" are written, please realize your netizens. In the following example, column1 is the primary key of the data table. 5 lines of data per page.
private void SetGridSource (int StartPosition, string GoToPage) {SqlConnection MyConnection = new SqlConnection (YourOwnConnectionString); SqlCommand MyCommand = null; switch (GoToPage) {case "Previous": MyCommand = new SqlCommand ( "SELECT TOP 5 * FROM Table WHERE Column1> = @ID ORDER BY Column1 ", MyConnection); if (StartPosition == 0) MyCommand.Parameters.add (" @ ID ", sqldbtype.nvarchar, 10) .Value =" "; // Here the parameter 10 is ID Length else mycommand.parameters.add ("@ ID", sqldbtype.nvarchar, 10) .value = viewstate [(DataGrid1.currentPageIndex 1) .tostring ()];
Break; Case "Next": mycommand = new Sqlcommand ("SELECT TOP 5 * from Table Where Column1> @ID ORDER BY Column1", MyConnection); // Note: This is>, not> = 哟 MyCommand. Parameters.add ("@ id", sqldbtype.nvarchar, 10) .Value = DataGrid1.items [4] .cells [0] .text; // ipems [4] indicates the last line of the last line of each page. } Myconnection.open (); sqldatarader dr = mycommand.executeRead (); DataGrid1.datasource = DR; DATAGRID1.DATABIND (); Dr.Close (); myconnection.close (); // with viewstate to cache just visited The primary key value of the first line of the first line ViewState [(DataGrid1.currentPageIndex 1) .tostring ()] = DataGrid1.Items [0] .Cells [0] .text;}
Note: If it is an Oracle database, you can use ROWNUM in the WHERE condition to control the number of strips and content.
Please call the setgridsource method, parameters in the final call of the secret Click event
StartPosition = DataGrid1.currentPageIndex * dataGrid1.pageSize;
.
----
Disclaimer: The right to copyright and interpretation of this article belongs to Han Rui, if you need to reprint, please keep your full content and this statement.
QQ: 18349592
E-mail: Henry7685@hotmail.com
Please visit my column: http://www.9cbs.net/develop/author/netauthor/latitude/