Henry Note: WinForm Datagrid Structure Analysis (3)

Source: Internet
Author: User

HenryShouji-WinForm DatagridStructure analysis (III)

Han Rui (2002-12-18)

 

Hello everyone, I am very happy to meet you again. I don't know how you know about the previous two articles about the Datagrid structure. The questions discussed today are based on them, therefore, I hope you can read the first two articles to obtain the necessary concepts.

Iii. Custom column classes

This section describes how to create a column Class and add it to the Datagrid for use. In the previous section, we discussed the DataGridTextBoxColumn class and generated a cell and row inherited from the DataGridTextBoxColumn that has changed the cell and row (that is, the set of cells in a row) dataGridColoredTextBoxColumn class with color changing capability. To create a column class, you should study the DataGridTextBoxColumn class again and create a new class accordingly. In this example, you need to create a column with a drop-down box (1 ), therefore, it should be the same as the DataGridTextBoxColumn class, that is, they should be derived from the same parent class, so as to ensure the similarity and consistency in use.

 

Figure 1 Datagrid with drop-down box columns

1. When will the drop-down box be used?

When will the drop-down box column be used? Generally, it is used only when the content entered in the cell is unknown. The data source may come from two data tables. One is the data table embodied in the Datagrid and the other is recorded in the drop-down list. (Of course, the content in the drop-down list can not be stored in the database, just for unified management. I am used to doing this ). In this example, the data source of the Datagrid is:

 

Figure 2 Datagrid Data Source

Here, the "customer" table is the source table of the content displayed in the Datagrid as a whole, because the "country" column is filled with the confirmed countries, therefore, the "country" table is used to record the country names displayed in the drop-down list.

2. Use of the drop-down Box Column

Now let's take a look at the use process of the drop-down box column:

Figure 3 use process in the drop-down box

From figure 3, we can easily understand the processing process of the drop-down box column. Under normal circumstances, the cell in this column is displayed as a textbox, the drop-down box is triggered when the textbox falls into the focus. Then, the cell is a real drop-down box. When the focus leaves the cell, the cell is restored to a textbox.

3. Create a class

The project is about to start. Let's take a look at our ideas and what should we do:

(1) inherit a Custom column class from the parent class of the datagridtextboxcolumn class: datagridcomboboxcolumn;

(2) Add a ComboBox instance to the column for the appearance of the focus, which is similar to the datagridtextbox class to which the textbox used by the datagridtextboxcolumn class belongs, we should design a dedicated ComboBox class for the use of the datagridcomboboxcolumn class.

(3) track the cell status. When focusing, add a ComboBox outside the textbox, hide the ComboBox and restore it to the textbox;

(4) override the edit and paint methods of the parent class to adapt to the use of ComboBox (for more information about the painting problem, see the previous section) in edit, the changes generated in ComboBox (user input or selected in the drop-down box) are recorded in the cell, which facilitates updating the changes to the relevant data source, in this example, we mainly use the following example to bind a drop-down box to a data source. Therefore, user input is not supported. Instead, we focus on the displaymember and valuemember attributes of ComboBox to reflect the relationship with the data source. This requires attention. If you want to implement user input at the same time (this is not the advantage of ComboBox, right ?) Modify the author's code.

For the class code and usage, see the articles "(3) class code" and "(3) Use Code.

This section describes the concept and work required to create a new column. The DataGrid structure analysis is coming to an end soon. This series is intended to inspire you and help you learn and work. Next, I will discuss with you the event response mechanisms and methods of the DataGrid and the advanced applications connected to the database. You can also write to me to inform me of your concerns about the DataGrid. I will discuss the issues with you in a more concentrated manner. I wish you all a better career!

----

Disclaimer: the copyright and interpretation rights of this article are owned by Han Rui. If you need to reprint it, please keep the complete content and this statement.

QQ: 18349592

E-mail: henry7685@hotmail.com

Please visit my column: http://www.csdn.net/develop/author/netauthor/Latitude/

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.