Henry Instruction: Winform DataGrid Structure Analysis (1)

zhaozj2021-02-16  54

Henry Instruction - Analysis of Winform DataGrid Structure (1)

Han Rui (2002-11-14)

I have long wanted to write this topic, but I have always had a doubt that I have been able to conduct a comprehensive analysis of DataGrid in the WinForm in .NET. However, there are more and more people who have asked related questions. The programmers used to use .NET are increasing, two is the use of DataGrid's use of DataGrid has got rid of the needs of the original display data, and wants to use advanced use. It should have a full understanding of the structure of DataGrid. This article hopes to help friends with this aspect.

First, basic structure

From the shape, the DataGrid is composed of multiple tables (Table), which is a single unit (Cell) consisting of rows (ROW) and columns, interacts with columns, composed. Our needs can control each Cell change, so that the ROW and Column have changed, and finally form a change in Table. Each of this variation is considered to be a style format of Table in DataGrid.

After deploying the DataGrid control on the Form, we will appear in its property window, and the changes in the background color (BackColor) and the forecolor and font (font) change. After this article, you will be able to achieve more format changes.

Describe the basic structure, first look at the content displayed in Figure 1:

However, DataGrid does not directly write data, and the data displayed in Figure 1 is determined by the DataGrid DataSource (data source). This DataSource is an object that supports the Ienumerable interface, such as ArrayList, Collection, DataView, DataRow, DataTable, and more. (This problem is not the focus of this article, temporary)

So what is the structure of DataGrid? I tried a structural map as shown in Figure 2:

For clarity, we mainly discuss DataGrid-> DataGridTableStyle-> DataGridColumnStyle. The default structure of our usual, that is, set the DataGridColumnStyle to the DataGridTextBoxColumn column structure, which makes the data of the DataGrid as consisting of Textbox. Therefore, we can see the effect shown in Figure 1, and each Cell is a TextBox. Similarly, we know if you set a column's DataGridColumnStyle to the DataGridBoolColumn column structure, you can display the value of the Boolean type with the checkbox control in this column. We can even customize the column type of a column, join Combox, etc., this is a detailed manner.

Then this section is the main discussion when Cell is the default TextBox, the changes in the attributes showing DataGrid, including: List, column width, foreground and background color, etc. In the subsequent section, the extended functions are described, including the DataGrid to implement the keyboard and the mouse response event, add a custom column style.

In Figure 1, I want to change the content of the column, is it convenient to change the header (Caption Text), as long as you write a sentence in the code:

DataGrid1.captiontext = "Henry Example" Unfortunately, it is not that simple. As we analyze it above, to control a column content and style, you must implement DataGridColumnStyle. So, start working:

'Build a simple DataTable as a data source of DataGrid

Label1.parent = DataGrid1

Label1.backcolor = color.transparent

DIM DT AS DATATABLE

DT = Dataset1.tables.add ("MyTable")

Dt.columns.Add ("Column 1", gettype (string))

Dt.columns.Add ("Column 2", GetType (Integer))

Dim Row, Row1 AS Datarow

Row = DT.NEWROW ()

Row! Column 1 = "Row 1"

Row! Column 2 = 1

Dt.Rows.Add (Row)

Row1 = DT.NEWROW ()

Row1! Column 1 = "Row 2"

Row1! Column 2 = 12

Dt.Rows.Add (Row1)

'Construction is completed

DIM TS AS New DataGridTableStyle () 'is what it determines what DataGrid is like

DIM acolumntextColumn as DataGridTextBoxColumn 'determines the style of each column

DataGrid1.datasource = DT 'Setting data source

Ts.mappingname = dt.tablename

DIM Numcols as INTEGER

Numcols = dt.columns.count 'Number of data sources

DataGrid1.captionText = "Henry Example"

DIM I as integer = 0

Do While (i

acolumnTextColumn = New DataGridTextBoxColumn ()

'To change the list name, please refer the Headertext value of the sentence

AcolumnTextColumn.Headertext = DT.COLUMNS (i) .columnname ☆

AcolumnTextColumn.mappingname = dt.columns (i) .columnname ☆

'Control column width and line

IF i = 1 THEN

Ts.preferredColumnWidth = 50

Ts.PreferredRowHeight = 20

END IF

Ts.alternatingbackColor = color.lightgray 'Sets the background color of the alternate row

Ts.GridColumnStyles.add (acolumntextColumn) 'Add a custom Column style

i = (i 1)

Loop

DataGrid1.tableStyles.Add (TS) 'Add a custom table style

'Note: After adding the style, you have added a new record in real time in DataGrid, and the style will not change.

When the TableStyle has defined DataGrid, the specific steps are as shown above, and the drawing of the step is: set the style of each column, if you want to use textbox, define one:

DIM AcolumnTextColumn As DataGridTextBoxColumn wants to control the list of column headers and their column corresponding database content, you must rewrite the HeadText and MappingName, but also two properties that must be declared, otherwise it will not be overwritten.

AcolumnTextColumn.Headertext corresponds to the list

AcolumnTextColumn.mappingName must be noted that it corresponds to the column name of the real database, so it cannot be changed casually.

After having these two properties, a column is generated, if you want to change the column width, use:

Ts.preferredColumnWidth = 50

If you want to hide a column, you will write:

Ts.preferredColumnWidth = 0 is simple!

If you want to make the column width according to the data content adaptive adjustment, you can do this:

AcolumnTextColumn.TextBox.autosize = TRUE

Ts.PreferredColumnWidth = acolumntextColumn.TextBox.width

Join the changed DataGridTextBoxCoulmn instance to GridColumnStyles, the code is:

Ts.GridColumnStyles.Add (AcolumnTextColumn)

After override each column (note that if you want to customize a DataGrid TableStyle, you must rewrite all the GridColumnStyle to each column), you can add the custom tableStyle to the tablestyle of the DataGrid:

DataGrid1.tables.Add (TS)

Through these steps, we control each Cell is part of the DataGrid of TEXTBOX (Cell's foreground color change in the next section in the next section)

So how do you join a checkbox column in DataGrid? It is not complicated relative to column that adds other control types.

First, we have to define a column with a Boolean type, in the above code, join:

Dt.columns.add ("Column 3", gettype (boolean))

Dim Row, Row1 AS Datarow

Row = DT.NEWROW ()

Row! Column 1 = "Row 1"

Row! Column 2 = 1

Row! Columns 3 = FALSE

Dt.Rows.Add (Row)

Row1 = DT.NEWROW ()

Row1! Column 1 = "Row 2"

Row1! Column 2 = 12

Row1! Column 3 = TRUE

Dt.Rows.Add (Row1)

Then you can use the following definition to embed CHECKBOX in the DataGrid, add the following code in the above code:

DIM AC as DataGridBoolcolumn = New DataGridBoolColumn ()

Ac.Headertext = dt.columns (2) .columnname

ac.mappingname = dt.columns (2) .columnname

Ts.GridColumnStyles.Add (AC)

Of course, you want to modify: do while (i

----

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

转载请注明原文地址:https://www.9cbs.com/read-25556.html

New Post(0)