Selecting the Top n Rows in a DataTable

[ Team LiB ] Recipe Selecting the Top n Rows in a DataTable Problem You want to create a grid that shows the t op five rows in a DataTable, based on the values in one of the columns. Solution Use an appropriate sort order with a DataView filter. | Team LiB Recipe Selecting the Top n Rows in a DataTable Problem You want to create a grid that shows the t op five rows in a DataTable based on the values in one of the columns. Solution Use an appropriate sort order with a DataView filter. The sample code contains two event handlers Sets up the sample by creating a DataTable containing the Orders table from the Northwind sample database. The default view of the table is bound to the data grid on the form. Select Builds a filter on the DataView to limit the number of rows to the user-specified count with the largest Freight values. The C code is shown in Example 3-10. Example 3-10. File Namespaces variables and constants using System using using using using using private DataView dv Table name constants private const String ORDERS_TABLE Orders Field name constants private const String ORDERID_FIELD OrderlD private const String FREIGHT_FIELD Freight . . . private void DataViewTopNSelectForm_Load object sender e Fill the Orders table. SqlDataAdapter da new SqlDataAdapter SELECT FROM Orders S ql_ConnectString DataTable dt new DataTable ORDERS_TABLE dt dt Get the default view for the table and bind it to the grid. dv dv private void selectButton_Click object sender e This example will select the top n freight values. Set the field name variable. String topNFieldName FREIGHT_FIELD int topN 0 try topN if topN 0 Enter an Integer greater than 0. return catch Enter an Integer greater than 0. return Clear the filter on the view. Sort the view descending on the top

Bấm vào đây để xem trước nội dung
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.