By default, the DataGridView effect is displayed:
1. Disable the last line of whitespace.
By default, the last line of whitespace represents an automatic new row, and for editing in the control, you can preserve
?
1 |
dataGridView1.AllowUserToAddRows = false ; |
The above disable is only the automatic new line to interact with the user interface is forbidden, but still can be through the code: DATAGRIDVIEW1.ROWS.ADD (), to add a line of blank.
2. Disable the delete function of the ' delete ' key.
By default, the mouse selects a whole row, and the delete key deletes the current entire row.
?
1 |
dataGridView1.AllowUserToDeleteRows = false ; |
The above is disabled, only the automatic new line that interacts with the user interface is forbidden, but still can be through the code:
?
1 |
dataGridView1.Rows.Remove(DataGridViewRow dataGridViewRow); |
Or
?
1 |
dataGridView1.Rows.RemoveAt( int index); |
To delete the specified row data.
3. Enable the mouse Drag and drop column function
When enabled, the order of the columns can be reordered by dragging the mouse. However, the drag-and-drop does not affect the column ordinal (keeping the original ordinal number) when the columns are accessed through code, but only shows the effect changes.
?
1 |
dataGridView1.AllowUserToOrderColumns = true ; |
4. Disable the mouse drag row height, column width
When disabled, the width of the column and the height of the row cannot be changed by mouse interaction. does not affect setting by code
?
12 |
dataGridView1.AllowUserToResizeColumns = false ; // 禁拖动列宽度 dataGridView1.AllowUserToResizeRows = false ; // 禁拖动行高度 |
5. Disable the mouse drag row header (leftmost blank column) width
Datagridview1.rowheaderswidthsizemode = datagridviewrowheaderswidthsizemode.disableresizing; enumeration, which can be enumerated bit-adaptive size
6. Disable the cell editing feature
?
1 |
dataGridView1.ReadOnly = true ; |
7. Click to select the entire row, the entire column
?
1 |
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; // 单击选中整行,枚举 |
SelectionMode is an enumeration type:
8. Disable multi-line/Dole/multi-cell selection
?
1 |
dataGridView1.MultiSelect = false ; |
9. Set the style of grid line color of table
?
1234 |
dataGridView1.AdvancedCellBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.InsetDouble; // 设置边框样式(上边框),枚举:双线内陷边框 // ... dataGridView1.GridColor = Color.SeaGreen; //边框线 颜色 |
10. Automatic line Numbering
There is no direct setting property, you need to render the event with the control: datagridview1.cellpainting+=datagridview1_cellpainting;
?
12345678910111213141516171819202122232425 |
//在单元格需要绘制时发生。
private void dataGridView1_CellPainting(
object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex < 0 && e.RowIndex >= 0)
// 绘制 自动序号
{
e.Paint(e.ClipBounds, DataGridViewPaintParts.All);
Rectangle vRect = e.CellBounds;
vRect.Inflate(-2, 2);
TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), e.CellStyle.Font, vRect, e.CellStyle.ForeColor, TextFormatFlags.Right | TextFormatFlags.VerticalCenter);
e.Handled =
true
;
}
// ----- 其它样式设置 -------
if (e.RowIndex % 2 == 0)
{
// 行序号为双数(含0)时
e.CellStyle.BackColor = Color.White;
}
else
{
e.CellStyle.BackColor = Color.Honeydew;
}
e.CellStyle.SelectionBackColor = Color.Gray;
// 选中单元格时,背景色
e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
//单位格内数据对齐方式
}
|
Display effect:
The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support the script home.
Article Source: https://www.jb51.net/article/121045.htm
C # Form Controls DataGridView Common settings