C # description of winform dview attributes

Source: Internet
Author: User

① Obtain or modify the content of the current cell
② Set cell read-only
③ Do not display the bottom New Line
④ Identify new rows
User-Defined deletion operations in Row 5
⑥ Hide and delete rows and columns
7. Disable column or row Resize
Adjust the column width and row height, the height of the column header, and the width of the row header automatically.
Rotate to freeze columns or rows
Sequence column order adjustment
Rows row header column header Cells
Operation of the clipboard
ToolTip settings of worker cells
Setting of the context menu (ContextMenuStrip)
Border and gridline style settings of cells
Limit cell indicates the Value Setting
When users enter a cell, set the input value of the cell.
Configure the default value of the newly added row

① DataGridView get or modify the content of the current cell:

GO TO TOP

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]
Obtains the content of the current cell.
Console. WriteLine (maid. Value)
Retrieve the column Index of the current cell
Console. WriteLine (maid. ColumnIndex)
Obtains the row Index of the current cell.
Console. WriteLine (maid. RowIndex)

[C #]
// Obtain the content of the current cell
Console. WriteLine (maid. Value );
// Retrieve the column Index of the current cell
Console. WriteLine (maid. ColumnIndex );
// Obtain the row Index of the current cell
Console. WriteLine (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)

[C #]
// Set (0, 0) to the current cell
Maid = maid [0, 0];
When the entire row selection mode is enabled, you can also set the selected row through CurrentCell.
/** // <Summary>
/// Traverse down
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void button4_Click (object sender, EventArgs e)
...{
Int row = this. Maid. currentrow. Index + 1;
If (row> this. dataGridView1.RowCount-1)
Row = 0;
This. Maid = this. Maid [0, row];
}

/** // <Summary>
/// Traverse up
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void button5_Click (object sender, EventArgs e)
...{
Int row = this. Maid. currentrow. Index-1;
If (row <0)
Row = this. Maid-1;
This. Maid = this. Maid [0, row];
}
* Note: The parameters of this. dataGridView indexer are columnIndex, rowIndex, columnName, and rowIndex.
This is different from habit.


--------------------------------------------------------------------------------
② DataGridView set cell read-only:

GO TO TOP

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
[C #]
// Set DataGridView1 to read-only
DataGridView1.ReadOnly = true; in this case, the user's new and Delete row operations are also blocked.

If you want to disable the editing of a cell in the DataGridView, you only need:
[VB. NET]
Set the cells in the entire column of Column 2nd of DataGridView1 to read-only.
DataGridView1.Columns (1). ReadOnly = True

Set the entire row of cells in the 3rd row of the DataGridView1 to read-only.
DataGridView1.Rows (2). ReadOnly = True

Set [0, 0] cells of DataGridView1 to read-only
DataGridView1 (0, 0). ReadOnly = True
[C #]
// Set the full-Column cell of Column 2nd of DataGridView1 to read-only.
DataGridView1.Columns [1]. ReadOnly = true;

// Set the entire row of cells in the 3rd row of the DataGridView1 to read-only.
DataGridView1.Rows [2]. ReadOnly = true;

// Set [0, 0] cells 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
[C #]
DataGridView1.EditMode = DataGridViewEditMode. 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
[C #]
// CellBeginEdit event handling method
Private void maid (object sender,
DataGridViewCellCancelEventArgs e)
{
DataGridView dgv = (DataGridView) sender;
// Check whether conditions can be edited
If (dgv. Columns [e. ColumnIndex]. Name = "Column1 "&&
! (Bool) dgv ["Column2", e. RowIndex]. Value)
{
// Cancel editing

Related Article

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.