Binding Data to a Web Forms DataGrid

[ Team LiB ] Recipe Binding Data to a Web Forms DataGrid Problem You want to bind the result set from a query to a DataGrid control. Solution Set the advanced properties of the DataGrid as demonstrated in the code for the Web Forms page as shown | Team LiB Recipe Binding Data to a Web Forms DataGrid Problem You want to bind the result set from a query to a DataGrid control. Solution Set the advanced properties of the DataGrid as demonstrated in the code for the Web Forms page as shown in Example 7-7. Example 7-7. File asp DataGrid id dataGrid style Z-INDEX 102 LEFT 16px POSITION absolute TOP 56px runat server AllowPaging True AllowSorting True AlternatingItemStyle BackColor FFFF99 AlternatingItemStyle asp DataGrid The code-behind file contains three event handlers and one method Calls the CreateDataSource method and binds data to the Web Forms DataGrid if the page is being loaded for the first time. CreateDataSource This method fills a DataTable with the Orders table from the Northwind sample database and stores the DataTable to a Session variable to cache the data source for the DataGrid. Gets the cached data from the Session variable updates the CurrentPageIndex of the DataGrid and binds the data to the grid. Gets the cached data from the Session variable sets the sort order of the default DataView for the data and binds that DataView to the grid. The C code for the code-behind is shown in Example 7-8. Example 7-8. File Namespaces variables and constants using System using using using private void Page_Load object sender e if CreateDataSource Orderld private DataTable CreateDataSource DataTable dt new DataTable Create a DataAdapter and fill the Orders table with it. SqlDataAdapter da new SqlDataAdapter SELECT FROM Orders DataConnectString dt Store data in session variable to store data between posts to server. Session DataSource dt return dt private void dataGrid_PageIndexChanged object source .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.