Creating Custom Columns in a Windows Forms DataGrid

[ Team LiB ] Recipe Creating Custom Columns in a Windows Forms DataGrid Problem You need to create a DataGrid having columns with custom formatting and attributes. Solution Use the DataGridTableStyle class. The sample code contains two event handlers | Team LiB Recipe Creating Custom Columns in a Windows Forms DataGrid Problem You need to create a DataGrid having columns with custom formatting and attributes. Solution Use the DataGridTableStyle class. The sample code contains two event handlers Sets up the sample by creating a DataAdapter and using it to fill a DataTable with data from the ProductID ProductName and Discontinued columns of the Products table in the Northwind database. The DataGridTableStyle class is used to define a data grid containing three custom columns two text boxes and one check box corresponding to the ProductID ProductName and Discontinued columns. Finally the default view of the Products DataTable is bound to the data grid on the form. Update Uses the DataAdapter created in the event handler to update changes made to the Products DataTable back to the database. The C code is shown in Example 7-18. Example 7-18. File Namespaces variables and constants using System using using using using private SqlDataAdapter da private DataTable dt . . . private void CustomColumnsInDataGridForm_Load object sender e Create the DataAdapter. String selectCommand SELECT ProductID ProductName Discontinued FROM Products da new SqlDataAdapter selectCommand S ql_ConnectString Use CommandBuilder to handle updates back to the data source. SqlCommandBuilder cb new SqlCommandBuilder da Retrieve the table schema. dt new DataTable Products dt Default the check box column to false. Discontinued .DefaultValue false Fill the table. dt Define the table for the grid. DataGridTableStyle ts new DataGridTableStyle Products Define and add the columns to the grid. DataGridTextBoxColumn productIDCol new DataGridTextBoxColumn ProductID .

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.