![Phân tích tư tưởng của nhân dân qua đoạn thơ: Những người vợ nhớ chồng… Những cuộc đời đã hóa sông núi ta trong Đất nước của Nguyễn Khoa Điềm](https://timtailieu.net/upload/document/136415/phan-tich-tu-tuong-cua-nhan-dan-qua-doan-tho-039-039-nhung-nguoi-vo-nho-chong-nhung-cuoc-doi-da-hoa-song-nui-ta-039-039-trong-dat-nuoc-cua-nguyen-khoa-136415.jpg)
Binding Data to a Web Forms DataGrid
Số trang: 5
Loại file: pdf
Dung lượng: 28.39 KB
Lượt xem: 10
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
[ Team LiB ] Recipe 7.4 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
Nội dung trích xuất từ tài liệu:
Binding Data to a Web Forms DataGrid [ Team LiB ]Recipe 7.4 Binding Data to a Web Forms DataGridProblemYou want to bind the result set from a query to a DataGrid control.SolutionSet the advanced properties of the DataGrid as demonstrated in the code for the WebForms page as shown in Example 7-7.Example 7-7. File: ADOCookbookCS0704.aspx The code-behind file contains three event handlers and one method:Page.Load 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.DataGrid.PageIndexChanged Gets the cached data from the Session variable, updates the CurrentPageIndex of the DataGrid, and binds the data to the grid.DataGrid.SortCommand 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: ADOCookbookCS0704.aspx.cs// Namespaces, variables, and constantsusing System;using System.Configuration;using System.Data;using System.Data.SqlClient;private void Page_Load(object sender, System.EventArgs e){ if(!Page.IsPostBack) { dataGrid.DataSource = CreateDataSource( ); dataGrid.DataKeyField = OrderId; dataGrid.DataBind( ); }}private DataTable CreateDataSource( ){ DataTable dt = new DataTable( ); // Create a DataAdapter and fill the Orders table with it. SqlDataAdapter da = new SqlDataAdapter(SELECT * FROM Orders, ConfigurationSettings.AppSettings[DataConnectString]); da.Fill(dt); // Store data in session variable to store data between // posts to server. Session[DataSource] = dt; return dt;}private void dataGrid_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e){ // Get the data from the session variable. DataView dv = ((DataTable)Session[DataSource]).DefaultView; // Update the current page for the data grid. dataGrid.CurrentPageIndex = e.NewPageIndex; // Bind the data view to the data grid. dataGrid.DataSource = dv; dataGrid.DataBind( );}private void dataGrid_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e){ // Get the data from the session variable. DataView dv = ((DataTable)Session[DataSource]).DefaultView; // Set the sort of the data view. dv.Sort = e.SortExpression; // Bind the data view to the data grid. dataGrid.DataSource = dv; dataGrid.DataBind( );}DiscussionThe DataGrid Web Form control retrieves tabular information from a data source andrenders it in a web page. The control supports functionality for selecting, editing,deleting, sorting, and navigating the data.The DataGrid must be bound to a data source such as a DataReader, DataSet, DataTable,or DataView. Any class that implements the IEnumerable interface can be bound. Theeasiest way to create a DataGrid control is to drag the DataList control onto the web pagedesign surface.The DataGrid control uses templates to display items, control layout, and providefunctional capabilities similar to the DataList as described in Recipe 7.3. The differencesare: • Item templates are created for columns in the grid rather for the entire grid. • A DataGrid column does not have an AlternatingItemTemplate, or a SelectedItemTemplate, or a SeparatorItemTemplate.To specify columns for and format the DataGrid, right-click on the DataList control onthe design surface and select Property Builder. The DataGrid can also be customized byediting the HTML directly.A variety of DataGrid columns can be specified, but by default, columns areautomatically generated based on the fields in the data source. The DataGrid supports thecolumn types described in Table 7-5. Table 7-5. DataGrid column types Column type DescriptionBoundColumn Specify the data source field to display as text. A command button in the grid that can invoke custom logicButtonColumn when clicked. A button that supports in-place editing. These buttons raiseEditCommandColumn events specific to in-place editing described in Table 7-6.HyperlinkColumn Displays the contents as a hyperlink. A custom layout based on a combination of HTML and WebTemplateColumn Server controls in a specified template.Among the events that the DataGrid supports are those designed to help implementcommon data editing and manipulation functionality. These events are described in Table7-6. Table 7-6. Common DataGrid events for editing and navigation Event Description Raised when the in-place editing Cancel button is clicked forCancelCommand( ) an item in the control. Raised when the in-place editing Delete button is clicked forDeleteCommand( ) an item in the control. Raised when the in-place editing Edit button is clicked for anEditCommand( ) item in the control. Raised when a button other than an in-place editing button isItemCommand( ) clicked. Raised when one of the page selection elements is clicked.PageIndexChanged( ) The AllowPaging property of the control mu ...
Nội dung trích xuất từ tài liệu:
Binding Data to a Web Forms DataGrid [ Team LiB ]Recipe 7.4 Binding Data to a Web Forms DataGridProblemYou want to bind the result set from a query to a DataGrid control.SolutionSet the advanced properties of the DataGrid as demonstrated in the code for the WebForms page as shown in Example 7-7.Example 7-7. File: ADOCookbookCS0704.aspx The code-behind file contains three event handlers and one method:Page.Load 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.DataGrid.PageIndexChanged Gets the cached data from the Session variable, updates the CurrentPageIndex of the DataGrid, and binds the data to the grid.DataGrid.SortCommand 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: ADOCookbookCS0704.aspx.cs// Namespaces, variables, and constantsusing System;using System.Configuration;using System.Data;using System.Data.SqlClient;private void Page_Load(object sender, System.EventArgs e){ if(!Page.IsPostBack) { dataGrid.DataSource = CreateDataSource( ); dataGrid.DataKeyField = OrderId; dataGrid.DataBind( ); }}private DataTable CreateDataSource( ){ DataTable dt = new DataTable( ); // Create a DataAdapter and fill the Orders table with it. SqlDataAdapter da = new SqlDataAdapter(SELECT * FROM Orders, ConfigurationSettings.AppSettings[DataConnectString]); da.Fill(dt); // Store data in session variable to store data between // posts to server. Session[DataSource] = dt; return dt;}private void dataGrid_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e){ // Get the data from the session variable. DataView dv = ((DataTable)Session[DataSource]).DefaultView; // Update the current page for the data grid. dataGrid.CurrentPageIndex = e.NewPageIndex; // Bind the data view to the data grid. dataGrid.DataSource = dv; dataGrid.DataBind( );}private void dataGrid_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e){ // Get the data from the session variable. DataView dv = ((DataTable)Session[DataSource]).DefaultView; // Set the sort of the data view. dv.Sort = e.SortExpression; // Bind the data view to the data grid. dataGrid.DataSource = dv; dataGrid.DataBind( );}DiscussionThe DataGrid Web Form control retrieves tabular information from a data source andrenders it in a web page. The control supports functionality for selecting, editing,deleting, sorting, and navigating the data.The DataGrid must be bound to a data source such as a DataReader, DataSet, DataTable,or DataView. Any class that implements the IEnumerable interface can be bound. Theeasiest way to create a DataGrid control is to drag the DataList control onto the web pagedesign surface.The DataGrid control uses templates to display items, control layout, and providefunctional capabilities similar to the DataList as described in Recipe 7.3. The differencesare: • Item templates are created for columns in the grid rather for the entire grid. • A DataGrid column does not have an AlternatingItemTemplate, or a SelectedItemTemplate, or a SeparatorItemTemplate.To specify columns for and format the DataGrid, right-click on the DataList control onthe design surface and select Property Builder. The DataGrid can also be customized byediting the HTML directly.A variety of DataGrid columns can be specified, but by default, columns areautomatically generated based on the fields in the data source. The DataGrid supports thecolumn types described in Table 7-5. Table 7-5. DataGrid column types Column type DescriptionBoundColumn Specify the data source field to display as text. A command button in the grid that can invoke custom logicButtonColumn when clicked. A button that supports in-place editing. These buttons raiseEditCommandColumn events specific to in-place editing described in Table 7-6.HyperlinkColumn Displays the contents as a hyperlink. A custom layout based on a combination of HTML and WebTemplateColumn Server controls in a specified template.Among the events that the DataGrid supports are those designed to help implementcommon data editing and manipulation functionality. These events are described in Table7-6. Table 7-6. Common DataGrid events for editing and navigation Event Description Raised when the in-place editing Cancel button is clicked forCancelCommand( ) an item in the control. Raised when the in-place editing Delete button is clicked forDeleteCommand( ) an item in the control. Raised when the in-place editing Edit button is clicked for anEditCommand( ) item in the control. Raised when a button other than an in-place editing button isItemCommand( ) clicked. Raised when one of the page selection elements is clicked.PageIndexChanged( ) The AllowPaging property of the control mu ...
Tìm kiếm theo từ khóa liên quan:
công nghệ thông tin kỹ thuật lập trình Oreilly Ado Dot Net Cookbook Ebook-Lib Binding Data to a Web Forms DataGridTài liệu liên quan:
-
52 trang 441 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 332 0 0 -
74 trang 310 0 0
-
96 trang 307 0 0
-
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 299 0 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 293 0 0 -
Tài liệu dạy học môn Tin học trong chương trình đào tạo trình độ cao đẳng
348 trang 291 1 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 281 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 279 0 0 -
Tài liệu hướng dẫn sử dụng thư điện tử tài nguyên và môi trường
72 trang 275 0 0