0. Ranks, width adaptive, plus numbering, background color.
Grid. Columns.Add ("id", "id"); Grid. columns["ID"]. DisplayIndex = 0;
for (int i = 0; i < grid. Rows.Count; i++)
{
Grid. Rows[i]. cells["ID"]. Value = i + 1;
}
Grid. Rowheadersvisible = false;
Grid. Allowusertoaddrows = false;
Grid. Rowsdefaultcellstyle.font = new Font ("Song Body", 8, fontstyle.regular);
Grid. autoSizeRowsMode = Datagridviewautosizerowsmode.allcells;
Grid. autoSizeColumnsMode = Datagridviewautosizecolumnsmode.allcells;
Grid. BackgroundColor = Color.cadetblue;
1. DataGridView current Cell Properties GET, change
2. DataGridView Edit Properties
3. DataGridView The new append row in the bottom of the list is not represented
4. DataGridView determines whether the currently selected row is a newly appended row
5. DataGridView Delete Line can be set
6. DataGridView not represented and deleted
1. Current cell properties Get, change
[C #]
' The value of the currently selected cell
Console.WriteLine (DataGridView1.CurrentCell.Value)
' The index value of the current column
Console.WriteLine (DataGridView1.CurrentCell.ColumnIndex)
' The row index value of the current cell
Console.WriteLine (DataGridView1.CurrentCell.RowIndex)
' Assigns the value at (0, 0) in the control to the current cell.
Datagridview1.currentcell =datagridview1[0, 0]
2.DataGridView Edit Properties
All cell Edit Properties
[C #]
' DataGridView1 read-only property
Datagridview1.readonly = True
Specify row and column cell edit properties
[C #]
Datagridview1.columns[1]readonly = True
DATAGRIDVIEW1.ROWS[2]. ReadOnly = True
Datagridview1[0, 0]. ReadOnly = True
To determine the editing properties of a cell based on criteria
When the value of Column2 in the following example is true, Column1 is set to editable
[C #]
private void Datagridview1_cellbeginedit (object sender, DataGridViewCellCancelEventArgs e)
{
if (this. Datagridview1.columns[e.columnindex]. Name.tostring (). Equals ("Column2"))
{
if (datagridview1["Column2", E.rowindex]. Value.tostring (). ToLower (). Equals ("true"))
{
datagridview1["Column1", E.rowindex]. ReadOnly = false;
}
Else
{
datagridview1["Column1", E.rowindex]. ReadOnly = true;
}
}
}
3.DataGridView the bottom column new append rows are not represented
[C #]
Datagridview1.allowusertoaddrows = False
4. Determine whether the currently selected row is a newly appended row
[C #]
if (DataGridView1.CurrentRow.IsNewRow)
{
Console.WriteLine ("Current line, new added row");
}
Else
{
Console.WriteLine ("Current line, not newly added row");
}
5. DataGridView Delete Line can be set
[C #]
Datagridview1.allowusertodeleterows = False
Determine if the current row is to be deleted based on criteria
[C #]
Code 1 private void Datagridview1_userdeletingrow (object sender, Datagridviewrowcanceleventargs e)
2 {
3
4
5
6 if (MessageBox.Show OK to delete?, delete confirmation, Messageboxbuttons.okcancel, messageboxicon.question). Equals (System.Windows.Forms.DialogResult.OK))
7 {
8
9}
Ten Else
11 {
E.cancel = true;
13}
14}
6. DataGridView not represented and deleted
The ranks do not represent
[C #]
' The first column of DataGridView1 does not indicate
Datagridview1.columns[0]. Visible = False
' The first line of DataGridView1 does not mean
Datagridview1.rows[0]. Visible = False
The column header section does not represent
[C #]
Datagridview1.columnheadersvisible = False
Datagridview1.rowheadersvisible = False
Specify row and column deletions
[C #]
DataGridView1.Columns.Remove ("Column1")
DataGridView1.Columns.RemoveAt (0)
DataGridView1.Rows.RemoveAt (0)
Select Rows to delete (multiple rows)
[C #]
' DataGridView1 Delete the selected row
foreach (DataGridViewRow R in Datagridview1.selectedrows)
{
if (!r.isnewrow)
{
DataGridView1.Rows.Remove (R);
}
}
7. DataGridView row Width Height set to cannot edit
8. DataGridView Automatic adjustment of the high row amplitude
9. DataGridView designated row and column freeze
DataGridView column Order change can be set
DataGridView Line Complex Selection
The rows, columns, and cells selected by DataGridView are obtained
7. DataGridView Row Width Height set to cannot edit