Henry Instruction - Analysis of Winform DataGrid Structure (3)
Han Rui (2002-12-18)
Hello everyone, I am very happy to meet you, I don't know how everyone knows how the previous two articles about the DataGrid structure. The problem discussed today is based on them, so the author wants you to watch the first two The article is to obtain the necessary concept.
Third, custom column class
This section is mainly to discuss how to built a column class with everyone and add it to the DataGrid. The DataGridTextBoxColumn class is discussed with everyone in the previous section and generates a DataGridColoredTextBoxColumn class that inherits from DataGridTextBoxColumn with changing Cells (That's a collection of Cells in one line). To implement a column class, you should study a DataGridTextBoxColumn class well again, and create a new class similar to this, this example is to create a column with a drop-down box (as shown in Figure 1), therefore, it It should be associated with the DataGridTextBoxColumn class, that is, they should be derived from the same parent class, so that similarity and consistency can be guaranteed.
Figure 1 DataGrid with pull-down frame
1. When will I use a drop-down box?
When will I use a drop-down box, in general, it is only a few possibilities that can be fill in Cells. Then, the data source may come from two data sheets, one is a data sheet embodied in the DataGrid, and the other is to record the content of the drop-down box. (Of course, the drop-down box list can also be not stored in the database, just for unified management, the author is accustomed to doing this). In this example, the data source of DataGrid is:
Figure 2 DataGrid data source
Among them, the "Customer" table is the source table of the entire DataGrid's overall content. Due to several countries filled in the "National" column, the author uses the "country" table to record in the drop-down list. National name for users to choose.
2. Down-down frame
Take a look at the use of the drop-down box:
Figure 3 Down-pull box usage process demonstration
From Figure 3, we can easily understand the processing of the drop-down frame, in the usual situation, the Cell in this column is displayed in the form of TextBox, and the display of the pull-down frame is stimulated in Textbox. After, then, the Cell is a real dropped box. When the focus leaves the Cell, Cell recovers to a TextBox look.
3. Production of class
It is necessary to start, and I will do something, what we have to do:
(1) Inheriting a custom column class from the parent class of the DataGridTextBoxColumn class: DataGridComboBoxColumn;
(2) Add a ComboBox instance in the column to focus, the DataGridTextBox class belonging to the TextBox class used by the DataGridTextBoxColumn class, we should design a dedicated ComboBox class to provide DataGridComboBoxColumn class.
(3) Tracking the Cell status, when it is focused, add a ComboBox outside TextBox, hide ComboBox after losing the focus, and restores to TextBox;
(4) Rewrive the EDIT and PAINT method of the parent class to accommodate the use of ComboBox (see the next section of the specific discussion of the Paint question) Changes in ComboBox (user input or in the drop-down box) in the EDIT Record into CELL, which is also easy to update to the relevant data source after the change, in this case, the author is mainly an example to implement the binding function of the drop-down box and the data source, so it does not support user input, but pay attention to ComboBox Displaymember with the ValueMember property, is used to reflect the relationship between the data source. This requires readers to pay attention, if you want to implement user input (this is not the advantage of ComboBox, right?) Needs to make appropriate modifications to the author's code. Class code and method of use "(3) class code" and "(3) use code".
This section only describes the idea of the new column from the concept perspective and the job you need. DataGrid structural analysis is also coming to a paragraph, this series is designed to throw bricks, hoping to give you a little beneficial inspiration, which is conducive to learning and work. Next, the author will discuss the event response mechanism and method of DataGrid with everyone, and advanced applications with database connections. You can also write to me, inform you about DataGrid's concern, the author will choose a new discussion with everyone more concentrated. I wish you all the best, and your career is!
----
Disclaimer: The right to copyright and interpretation of this article belongs to Han Rui, if you need to reprint, please keep your full content and this statement.
QQ: 18349592
E-mail: Henry7685@hotmail.com
Please visit my column: http://www.9cbs.net/develop/author/netauthor/latitude/