Henry Instruction - Analysis of Winform DataGrid Structure (2)
Han Rui (2002-12-5)
In addition, it is too busy, and there is no time to write relay. However, he continued to receive the encouragement and inquiries of netizens, but they had to forget the tiredness of recent, hurried up, I hope that everyone will not be too disappointed.
Second, custom function
The previous section is the basic structure of DataGrid, which is already enough for general questions, but encounters some special needs, such as wanting a CELL color change, want to join a custom control column in DataGrid ( For example, ComboBox, you need to work with the content of the previous section. Today, let's talk about how to change a Cell, or a row of background colors to make everyone skilled in the DataGrid operation.
Figure
1
Discharge example diagram
In Figure 1, I have changed three colors of three Cells, but their excitation mechanisms are different, "US", "China" two CELL I use the specified Cell location to make it Callo, and "3" this Cell is the CELL I found to find greater than 2 in the data source and make it discolored, resulting. They will be described in detail below.
1. CELL screen finding work steps
In the first quarter, I showed you the structure of DataGrid, which mentioned that DataGrid is determined by DataGridTableStyle to determine its appearance. DataGridTableStyle is composed of DataGridColumnStyle. The default DataGridColumnStyle is a text frame, background color White, the foreground is black, this is the pre-work of our face. We have to find ways to adjust, and another premise of adjustment is the work, it does not support the toning of CELL in the end?
Remember the code I listed in the next section? (If you don't remember, please open the original text to look at it), I am re-painting the DataGrid to get the list, the column width changes, use:
DIM acolumntextColumn as DataGridTextBoxColumn 'determines the style of each column
It is what I said by the default column style. If you want to make a surgery to change its appearance, you have to perform surgery. Please wear gloves, and launch a quick knife, we start to dissect the DataGridTextBoxColumn class.
The role of this class is to carry the TextBox control in the cell of DataGridColumnStyle to edit strings.
We use several properties in the first quarter: HeadText and mappingname, and more. They are all applications for existing classes, which is not much helpful to our work today. We have to see how this class can display the user input or load. This must be aware of its "protected method".
After you change the properties of an instance acolumnTextColumn of the DataGridTextBoxColumn class, you will use the following sentence:
Ts.GridColumnStyles.Add (AcolumnTextColumn) To add an AcolumnTextColumn to GridColumnStyle in the DataGrid TableStyle (TS), which can then use the properties and methods of the DataGridTextBoxColumn class.
The steps display are nothing more than four steps:
a. Get position to be displayed in DataGrid
b. Get data to display in this position
c. Properties of TextBox used when you want to display
d. Start to draw on the screen
The first two steps are not very well, we don't need to change what, in this class, if the DataGrid is binded to a data source, it is to remove data from the specified location of the specified data source with the GetColumnValueatrow method, (data in the DataGrid) After the user's real-time editing changes, use the setColumnValueatrow method to return the value to the data source to maintain the synchronization of the data. If it is user real-time input data, use the Edit method to pass the real-time data to the Cell. Then call the Paint method to record the parameters of the drawing column, and finally take the color of Cell in the column through PaintText. Here, this will be explained here, and the Paint is automatically controlled and called by the system. When the DataGrid is displayed on the screen, when the DataGrid appearance occurs, it will automatically call the run, Used to maintain its appearance.
Look, it is not complicated, then how do we change? Of course, I want to join our own code when I interact with PaintText, come to a "raccoon to change the Prince".
2. Change work ideas
So the work we have to do will work accordingly:
a. Heavy duty PAINT method
b. Implement the work to be completed, including the character to be displayed, the properties of TextBox (this is the focus of work)
c. Transfer working parameters to PaintText and stimulate it.
Then the steps of the work will fall, first look at it, do you need to draw on the screen, what parameters do you need to PaintText?
PaintText methods have two definitions:
A. Draw text and borders in a given alignment in a given location.
Overloads Protected Sub PaintText (Graphics, Rectangle, String, Boolean)
B. Draw your text and border with the specified colors and alignment in the specified location.
Overloads Protected Sub PaintText (Graphics, Rectangle, String, Brush, Brush, Boolean)
The work we have to do is to change the color scheme of the text box, of course, it is to use B definition method, its detailed explanation is:
Overloads protected subpainttext (_
ByVal g as graphics, _
Byval TextBounds as Rectangle, _
Byval text as string, _
Byval backbrush as brush, _
Byval forebrush as brush, _
BYVAL Aligntoright As Boolean_
)
parameter
g: A Graphics object that is used to draw strings.
TextBounds: A Rectangle that contains the boundary data of the rectangle.
TEXT: To draw a string on the screen.
Backbrush: A brush, it determines the background color of the rectangle
Forebrush: A brush, it determines the foreground color of the rectangle.
Aligntoright: Indicates if the text is the value of right align.
Look, the properties we need: the background color and the foreground are so showed! Then we define the foreground and background colors of the CELL in Pain, is it not OK?
Paint also has three definitions, and the definition of the above PaintText is:
Overrides overloads protected friends sub paint (_BYVAL G As Graphics, _
Byval Bounds as Rectangle, _
Byval Source as currencymanager, _
Byval rownum as integer, _
Byval backbrush as brush, _
Byval forebrush as brush, _
BYVAL Aligntoright As Boolean_
)
As mentioned above, the Paint is automatically controlled by the system, so it will constantly complete some necessary work, such as reading data, defining parameters of PaintText. As defined above, the parameters of PaintText are more than two parameters: Source and Rownum. What are they doing? Remember the first two steps of the screen of the screen that I said in the above? Source represents the data source, the Rownum marks the location of the Cell in the DataGrid. Wait, the location of the Cell in DataGrid should be determined by the line and column two variables, why is there only one variable here?
This is the work mechanism of Paint himself. I introduced the "Paint method to record the parameters", "means that the system is called Paint to draw! Therefore, the system automatically controls Paint from the first column to the last column, as a Paint itself, of course, as long as the downlink can control the Cell! This is what we can change the true truth of the CELL look.
We call GetColumnValueatrow (Source, Rownow) to read data from the Source with the function of the system increment number, and then assign a value by Cell.
There is a fun thing, after I control the refreshing screen display process, I found that the system drawing is actually not a list, but a line of pictures. Why is this? In fact, this is not conflict with us. This is related to the display refresh mode of the screen. In fact, the system is saved in the order, then the data is cached, then displayed outwards outwards! This means that it is to give some friends who like to have a wake up. See Figure 2:
Figure 2 Refresh Process Demo
Implementation code and processing skills see: "Winform DataGrid Structure Analysis (2) Program".
After the discussion of this article, I hope that you have already mastered part of the DataGrid to achieve a secret! Next article I will bring you more comprehensive information, it is no longer satisfied with the transformation of a class, but a custom column style class. Please give more encouragement and criticism. Next!
----
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/