Henry notes the analysis of the structure of-winform DataGrid (i)

Source: Internet
Author: User
Tags define header integer
Henry notes the analysis of the structure of-winform DataGrid (i)

Han Rui (2002-11-14)



Have long wanted to write this topic, but have been wondering whether the DataGrid in the WinForm of. NET has been able to perform a comprehensive analysis. But more and more people are asking questions lately, One is the use of. NET programmers in the increase, and secondly is that the use of the DataGrid has been out of the original display data requirements, want to make a high-level use, you should have a full understanding of the structure of the DataGrid, this article hopes to have the needs of the friends a little help.

I. BASIC structure

In shape, a DataGrid is made up of multiple tables (table), which consist of rows and columns (column), and rows and columns that are composed of cells (cell). Our need is to be able to control the changes in each cell, so that the row and column changes, resulting in a table changes. Each of these changes can be considered a stylistic format (style) for a table in the DataGrid.

After we deploy the DataGrid control to the form, the autoformat appears below its Properties window, which changes mostly in the background color (BackColor) and the foreground color (Forecolor) and fonts (font). After the story of this article, you will be able to achieve more formatting changes.

To describe the basic structure, first look at Figure 1:




However, the DataGrid does not directly write data, and the data shown in Figure 1 is determined by the DataSource (data source) of the DataGrid. This datasource is an object that supports IEnumerable interfaces, such as Arraylist, Collection, Dataview, Datarow, DataTable, and so on. (This issue is not the focus of this article, temporarily skipped)

So what exactly is the structure of the DataGrid? I tried to draw a structure diagram as shown in Figure 2:


As a clear view, we mainly discuss the Datagrid->datagridtablestyle->datagridcolumnstyle. We normally see the default structure DataGrid, which is to set the DataGridColumnStyle as a datagridtextboxcolumn column structure, and set the DataGrid column to consist of a TextBox. So we can see the effect shown in Figure 1, each cell is a textbox. By the same token, we know that if you set the DataGridColumnStyle of a column to a DATAGRIDBOOLCOLUMN column structure, you can display and change the Boolean value in that column with a CheckBox control. We can even customize the column type of a column, join the Combox and so on, this content in later text will be detailed.

This section focuses on the changes in the properties of the DataGrid when the cell is the default textbox, including column headers, width, foreground and background colors. In later sections, the extended functionality is described, including implementing keyboard and mouse response events in the DataGrid, and adding custom column styles.

In Figure 1, I want to change the contents of the column header, not as convenient as changing the header (caption text), just write one sentence in the code:

DataGrid1. CaptionText = "Henry Example" on the line?

Unfortunately it's not that simple, as we've analyzed above, to control the content and style of a column, you have to change the DataGridColumnStyle to implement it. So, let's get started:

' Build a simple DataTable as a data source for the 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 = "Line 1"

row! column 2 = 1

Dt. Rows.Add (Row)

Row1 = dt. NewRow ()

row1! column 1 = "Line 2"

row1! Column 2 = 12

Dt. Rows.Add (ROW1)

' Construction completed



Dim ts as New DataGridTableStyle () ' That's how it determines what the DataGrid is.

Dim Acolumntextcolumn as DataGridTextBoxColumn ' determines the style of each column

DataGrid1.DataSource = DT ' Set data source

Ts. MappingName = dt. TableName



Dim Numcols as Integer

Numcols = dt. Columns.count ' number of columns in the data source

Datagrid1.captiontext = "Henry Example"

Dim i as Integer = 0



Do while (I < numcols) ' Redraw all columns '

Acolumntextcolumn = New DataGridTextBoxColumn ()

' To change the column header names, change the HeaderText value of the following sentence

Acolumntextcolumn.headertext = dt. Columns (i). columnname☆

acolumntextcolumn.mappingname = dt. Columns (i). columnname☆

' Control column widths and ruled

If i = 1 Then

Ts. Preferredcolumnwidth = 50

Ts. Preferredrowheight = 20

End If

Ts. AlternatingBackColor = Color.lightgray ' Sets the background color of alternating rows

Ts. Gridcolumnstyles.add (acolumntextcolumn) ' Add a custom column style

i = (i + 1)

Loop

DATAGRID1.TABLESTYLES.ADD (TS) ' Adds a custom table style

' Note: When you add style, you add new records in the DataGrid and the style still stays the same.



When you have defined the TableStyle of the DataGrid, the steps, as shown in the above code, are as follows: Set the style of each column first, and if you want to use a textbox, define one:

Dim Acolumntextcolumn as DataGridTextBoxColumn

To control the column headers and the database contents of each column, you must rewrite Headtext and MappingName, as well as the two attributes that must be declared, otherwise you cannot rewrite them.

Acolumntextcolumn.headertext corresponds to the column header.

Acolumntextcolumn.mappingname must be aware that it corresponds to the real database column name, so you can not arbitrarily change.

With these two properties, a column is generated, and if you want to change the column width, use the following:

Ts. Preferredcolumnwidth =50

If you want to hide a column, write it like this:

Ts. Preferredcolumnwidth =0 is very simple!

If you want the column widths to adjust to fit the data content, you can do this:

AColumnTextColumn.TextBox.AutoSize = True

Ts. Preferredcolumnwidth = AColumnTextColumn.TextBox.Width

Add the changed DATAGRIDTEXTBOXCOULMN instance to the GridColumnStyles, and the code is:

Ts. Gridcolumnstyles.add (Acolumntextcolumn)

After each column is rewritten (note that if you want to customize the TableStyle of a DataGrid, you must override each column's GridColumnStyle), Add the custom TableStyle to the TableStyle in the DataGrid:

DATAGRID1.TABLESTYLES.ADD (TS)

With these steps, we control the partial properties of this DataGrid in which each cell is a textbox (the foreground color of the cell and the background color changes are discussed in the next section).

So how do you add a checkbox column to the DataGrid? It is still not complex relative to column added to other control types.

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

Dt. Columns.Add ("Column 3", GetType (Boolean))

Dim row, row1 as DataRow

row = dt. NewRow ()

row! column 1 = "Line 1"

row! column 2 = 1

row! column 3 = False

Dt. Rows.Add (Row)

Row1 = dt. NewRow ()

row1! column 1 = "Line 2"

row1! Column 2 = 12

row1! column 3 = True

Dt. Rows.Add (ROW1)

You can then insert a checkbox in the DataGrid with the following definition, adding the following code in the code above:

Dim ac as DataGridBoolColumn = New DataGridBoolColumn ()

Ac. HeaderText = dt. Columns (2). ColumnName

Ac. MappingName = dt. Columns (2). ColumnName

Ts. Gridcolumnstyles.add (AC)

Of course, modify: Do While (I < numcols) is do while (I < numCols-1). Then add the above three code after the loop. Re-use: DATAGRID1.TABLESTYLES.ADD (TS) end.

The final effect chart is:


----

Disclaimer: This copyright and interpretation of the right to Han Rui all, if necessary reprint, please retain the full content and this statement.





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.