Source Datagrid Control

Windows DataGridColumns User Controls


Sample Image

Introduction

This article explains how to build some special user controls for DataGrid columns on a Windows application. The special controls are:DataGrid ComboBox, DateTimePicker and Button columns. In a DataGrid, a column style is an object that defines what the column looks and behaves like, including such things as color, font, and the presence of controls that will handle linked field in a database with the use of a ComboBox, a CheckBox and other controls. The .NET Framework includes two types of column-style classes by default: theDataGridTextBoxColumn and DataGridBoolColumn classes. A quantity of jobs you may want to achieve with the Windows FormsDataGrid control are, unexpectedly, more troublesome than you might expect. Principally, this is because the Windows Forms DataGridcontrol is column-based, rather than cell-based. As a result, to attain most tasks, you have to work with the columns, not the cells themselves. Hence the need to create our own set of DataGridColumnStyle objects that define custom column styles for the Windows Forms DataGrid control and add them to the GridColumnStylesCollection. Below is an explanation of how those three user controls might be useful:

DataGrid ComboBox Column Style

Suppose you have a database as shown below:

Student
Id
Name
Book
StudentId
BookName
DateTime

You have "StudentId", a foreign key to the "Id" field in "Student" table, and another field "BookName". Now, suppose you want to display the data of "Book" on a DataGrid, and don't want to display "StudentId" as integer. In addition, you need to display the name, but any editing would update the "StudentId" field. The first solution would be to create two DataTables with parent-child relation, and write many instructions to display the wanted fields correctly, and update the table as needed. This needs a lot of instructions and design. Another solution would be to use a combo box, with data source as the "Student" table, display member as "Name", and value member as "Id". And any edits would display the new name, but update the "StudentId" field. Using the DataGridComboBoxColumn class is easy to do that, and is added as a column to the DataGrid easily.

DataGrid DateTimePicker Column Style

Let's take the same database example as above. In the table "Book", there's a field "DateTime" with date information. Suppose you want to edit this field in the DataGrid directly. One way is to write the date, in the right format, in the text box column of the DataGrid. This could cause errors if you mistyped the date, or in a different format. Another solution would be to use a DateTimePicker control, and just choose the exact date from the control. Using the DataGridTimePickerColumn allows you to achieve that easily.

DataGrid Button Column Style

You are viewing all rows in the DataGrid. Now, suppose you want to delete a row from the DataSet. A simple way would be to click on a button on this row, and an event is fired to delete the row, then refresh the DataSet. This can be easily done usingDataGridButtonColumn.

Coppyright : http://www.codeproject.com/KB/grid/WindowsDataGridColumns.aspx

->Read More...

0 Comments:

Đăng nhận xét