Operations of the datagridview in VB.net

Source: Internet
Author: User

Directory:
1. Get or modify the content of the current cell
2. Set cell read-only
3. Do not display the bottom New Line
4. determine new rows
5. User-Defined deletion operations of rows
6. Hide and delete rows and columns
7. Disable column or row resize
8. Automatic Adjustment of column width and Row Height, column header height, and row header width
9. Freeze columns or rows
10. Column order adjustment
11. Row header Cells
12. clipboard operation
13. Cell tooltip settings
14. Set the right-click menu (contextmenustrip)
15. Cell border and gridline style settings
16. Cell represents the value setting
17. Cell input value setting when users enter
18. Set the default value of the newly added line

1. datagridview gets or modifies the content of the current cell:
The current cell refers to the cell with the focus of the dview. It can be obtained through the currentcell attribute of the datagridview object. If the current cell does not exist, nothing (C # Is null) is returned)
[VB. NET]
'Get the content of the current cell MessageBox. Show (maid. value)
'Get the column index of the current cell
MessageBox. Show (maid. columnindex)
'Get the row index of the current cell
MessageBox. Show (maid. rowindex)
In addition, use the datagridview. currentcelladdress attribute (instead of directly accessing the cell) to determine the row where the cell is located: datagridview. currentcelladdress. Y and column: datagridview. currentcelladdress. X. This is useful in avoiding sharing of shared rows.
The current cell can be changed by setting the currentcell of the datagridview object. You can use currentcell to set
The activation cell of the datagridview. Set currentcell to nothing (null) to unactivate the cell.
[VB. NET]
'Set (0, 0) to the current cell
Maid = maid (0, 0)
--------------------------------------------------------------------------------
2. Set cell read-only for the datagridview:
1) Use the readonly attribute
If you want to disable editing for all cells in the datagridview, you only need:
[VB. NET]
'Set datagridview1 to read-only
Datagridview1.readonly = true
If you want to disable the editing of a cell in the datagridview, you only need:
[VB. NET]
'Set the full column cell of Column 2nd of the maid to read-only
Datagridview1.columns (1). readonly = true
'Set the entire row of cells in the 3rd row of the maid to read-only
Datagridview1.rows (2). readonly = true
'Set the [0, 0] Cell of datagridview1 to read-only
Datagridview1 (0, 0). readonly = true
2) use the editmode attribute
When the datagridview. editmode attribute is set to datagridvieweditmode. editprogrammatically, you cannot manually edit the cell content. However, you can use a program to call the datagridview. beginedit method to enable the cell to enter the editing mode for editing.
[VB. NET]
Datagridview1.editmode = maid mode. editprogrammatically
3) set the uneditable status of cells based on the conditions.
When it is too difficult to set the readonly attribute of cells by cell coordinates one by one, you can cancel cell editing through the cellbeginedit event.
[VB. NET]
'Cellbeginedit event handling method
Private sub maid (byval sender as object ,_
Byval e as datagridviewcellcanceleventargs )_
Handles datagridview1.cellbeginedit
Dim dgv as datagridview = ctype (sender, datagridview)
'Can I perform a condition check for editing?
If dgv. Columns (E. columnindex). Name = "column1" andalso _
Not cbool (dgv ("column2", E. rowindex). Value) then
'Cancel editing
E. Cancel = true
End if
End sub
--------------------------------------------------------------------------------
3. The bottom new line is not displayed in the datagridview:
Generally, the bottom row of the datagridview is the newly appended row (row header *). If you do not want the user to append a row or display the new row, you can set the allowusertoaddrows attribute of the datagridview object to false.
[VB. NET]
'You cannot manually Add a new row to datagridview1.
Datagridview1.allowusertoaddrows = false
Supplement: If the datasource of the datagridview is bound to dataview, you can also set dataview. allowadd
The property is false to achieve the same effect.
--------------------------------------------------------------------------------
4. Determine the new row in the datagridview:
When the allowusertoaddrows attribute of the datagridview is true, that is, when the user is allowed to append a new row, the last row of the datagridview is the newly appended row (* row ). You can use the datagridviewrow. isnewrow attribute to determine which row is newly appended. In addition, you can use datagridview. newrowindex to obtain the row serial number of a new row. Newrowindex =-1 when no new row exists. [VB. NET]
If maid. isnewrow then
Console. writeline ("new row appended to the current behavior. ")
Else
Console. writeline ("the current row is not a new append row. ")
End if
--------------------------------------------------------------------------------
5. Customize the user deletion operation for the datagridview row:
1) unconditionally restrict row deletion.
By default, datagridview allows you to delete rows. If the allowusertodeleterows attribute of the datagridview object is set to false, the row deletion operation is disabled.
[VB. NET]
'Disable row deletion of maid.
Datagridview1.allowusertodeleterows = false
However, the row can still be deleted through the datagridviewrowcollection. Remove operation.
Supplement: If the datagridview is bound with dataview, you can use dataview. allowdelete to control row deletion.
2) condition judgment for row deletion.
When you delete a row, the datagridview. userdeletingrow event is triggered. In this event, you can determine the condition and cancel the delete operation.
[VB. NET]
'Userdeletingrow event of maid
Private sub maid userdeletingrow (byval sender as object ,_
Byval e as datagridviewrowcanceleventargs )_
Handles datagridview1.userdeletingrow
'Confirm the user before deletion.
If MessageBox. Show ("are you sure you want to delete this row of data? "," Confirm deletion ",_
Messageboxbuttons. okcancel, messageboxicon. Question) <> _
Windows. Forms. dialogresult. OK then
'If it is not OK, cancel.
E. Cancel = true
End if
End sub
--------------------------------------------------------------------------------
6. Hide and delete rows and columns of the datagridview:
1) Hide rows and columns
[VB. NET]
'Hide the first column of maid
Datagridview1.columns (0). Visible = false
'Hide the first line of maid
Datagridview1.rows (0). Visible = false
2) Hide the row header and column header
[VB. NET]
'Column header hiding
Maid = false
'Line header hiding
Maid = false
3) Delete rows and columns
[VB. NET]
'Delete a column named "column1"
Datagridview1.columns. Remove ("column1 ")
'Delete the first column
Datagridview1.columns. removeat (0)
'Delete the first line
Datagridview1.rows. removeat (0)
4) Delete the selected row
[VB. NET]
For each R as datagridviewrow in datagridview1.selectedrows
If not R. isnewrow then
Datagridview1.rows. Remove (r)
End if
Next
--------------------------------------------------------------------------------
7. Disable column or row resize in the datagridview:
1) Disable resize of all columns or rows.
[VB. NET]
'Prohibit the user from changing the column width of all columns of datagridview1
Datagridview1.allowusertoresizecolumns = false
'Prohibit the user from changing the Row Height of all rows in the maid
Datagridview1.allowusertoresizerows = false
2) do not specify the resize of a row or column.
[VB. NET]
'Prohibit the user from changing the column width of the first column of maid
Maid (0). resizable = maid. False
'Prohibit the user from changing the row width of the first column of maid
Maid (0). resizable = maid. False
About noset
When the resizable attribute is set to datagridviewtristate. notset, the attribute values of allowusertoresizecolumns and allowusertoresizerows of the datagridview are set by default. For example, when datagridview. allowusertoresizecolumns is set to false and resizable is set to noset, resizable = false.
Determine whether resizable inherits the attribute values of allowusertoresizecolumns and allowusertoresizerows of the datagridview. You can determine whether resizable inherits the attribute values based on the state attribute. If the State Property contains resizableset, it indicates that no inheritance settings are available.
3) set the minimum values of the column width and Row Height.
[VB. NET]
'The minimum column width of the first column is set to 100.
Datagridview1.columns (0). minimumwidth = 100
'The minimum Row Height of the first row is set to 50.
Datagridview1.rows (0). minimumheight = 50
4) You are prohibited from changing the width of the row header and the height of the column header.
[VB. NET]
'Prohibit the user from changing the height of the column header
Datagridview1.columnheadersheightsizemode = _
Datagridviewcolumnheadersheightsizemode. disableresizing
'Prohibit the user from changing the width of the line Header
Datagridview1.rowheaderswidthsizemode = _
Datagridviewrowheaderswidthsizemode. enableresizing
--------------------------------------------------------------------------------
8. Automatic Adjustment of the datagridview column width and Row Height:
1) set the Row Height and column width to automatically adjust
[VB. NET]
'Set the column width of the header and all cells to be automatically adjusted.
Datagridview1.autosizecolumnsmode = datagridviewautosizecolumnsmode. allcells
'Set the automatic adjustment of the Row Height including the header and all cells
Datagridview1.autosizerowsmode = datagridviewautosizerowsmode. allcells
2) automatic adjustment of specified columns or rows
[VB. NET]
'Automatic adjustment of the first column
Datagridview1.columns (0). autosizemode = _
Datagridviewautosizecolumnmode. displayedcells
3) set the height of the column header and the width of the row header to automatically adjust [VB. NET]
'Set the column header width to be adjusted freely
Datagridview1.columnheadersheightsizemode = _
Datagridviewcolumnheadersheightsizemode. autosize
'Set the width of the line header to be adjusted freely.
Datagridview1.rowheaderswidthsizemode = _
Datagridviewrowheaderswidthsizemode. autosizetoallheaders
4) automatic adjustment at any time
A. temporarily adjust the column width automatically, which is the same as the autosizecolumnsmode attribute. [VB. NET]
'Automatically adjust the width of all columns of datagridview1.
Datagridview1.autoresizecolumns (datagridviewautosizecolumnsmode. allcells)
'Automatically adjust the column width of the first column of maid.
Datagridview1.autoresizecolumn (0, datagridviewautosizecolumnmode. allcells)
B. Temporary, automatic adjustment of the Row Height
[VB. NET]
'Automatically adjust the height of all rows of datagridview1.
Datagridview1.autoresizerows (datagridviewautosizerowsmode. allcells)
'Let the Row Height of the first row of the maid be adjusted automatically.
Datagridview1.autoresizerow (0, datagridviewautosizerowmode. allcells)
C. Temporarily, automatically adjust the row header and column header
[VB. NET]
'The column header height is automatically adjusted
Datagridview1.autoresizecolumnheadersheight ()
'Automatic line header Width Adjustment
Datagridview1.autoresizerowheaderswidth (_
Datagridviewrowheaderswidthsizemode. autosizetoallheaders)
Performance:
When auto-tuning is performed on cells specified by the autosizecolumnsmode or autosizerowsmode attribute, too many adjustments may lead to performance degradation, especially when there are many rows and columns. At this time, replacing allcells with displayedcells can reduce the adjustment of cells that are not seen, thus improving performance.

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.