WinForm: DataGridView newly added row

1. The new bottom line does not show

The bottom row is generally DataGridView user newly added row (first line displayed *). If you do not want users do not want to display the new additional line that is the new line, AllowUserToAddRows property DataGridView object can be set to False.

DataGridView1.AllowUserToAddRows = false;

However, you can program: DataGridViewRowCollection.Add for the DataGridView to add a new row.

2. Add the default values ​​for the new generation of row

When the user selects the "new line" as the current line, DataGridView will trigger DefaultValuesNeeded event. In this event, you can access a new line, and its default value is generated for the convenience of the user input.

private void dataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
{
    e.Row.Cells["Region"].Value = "WA";
    e.Row.Cells["City"].Value = "Redmond";
    e.Row.Cells["PostalCode"].Value = "98052-6399";
    e.Row.Cells["Region"].Value = "NA";
    e.Row.Cells["Country"].Value = "USA";
    e.Row.Cells["CustomerID"].Value = NewCustomerId();
}

3. Enter the data in a new line

Users begin before the new line of input data, IsNewRow properties of the new line is true, that is, the first trigger diverted CellEnter and CellBeginEdit events IsNewRow events Ture. Once the user starts typing, this line is no longer a new line, DataGridView will produce a "new" new line, look at the following diagram:

Adding "new" new line will trigger UserAddedRow event, its event handler of the second parameter has attributes Row, designated the "new" new line. If the user then press the Escape key, the "new" new line will be removed, which will trigger UserDeletingRow events, properties Row its event handler for the second parameter specifies the "new" new line.

4. custom visual effects of the new line

  • The new line is based on a template created RowTemplate, if it is not specified cell style, they will use inherited styles.
  • The initial value of the cells in the new row is determined by DefaultNewRowValue attribute of each cell. For DataGridViewImageCell cell types, the initial value is a placeholder image, compared with other types of null. You can override this property to return a custom value. But it can be replaced with the default value in DefaultValuesNeeded event handler, the event focus into a new row trigger.
  • The new line is the title of the standard arrow icon or an asterisk, and have not been exposed. If you want to customize the icon, you need to create a custom DataGridViewRowHeaderCell class.
  • Use standard icons header cell DataGridViewCellStyle the ForeColor property title of the new line. Note: If there is not enough space, the icon will not be displayed.
  • If you set the string value for the header cell (via the Value property), but there is not enough control while displaying text and icons, the icon will be cut off first.

The new line ordering

  • In unbound mode, always add a new line in the last row of the DataGridView, even if you have to sort the data. Users will need to "Auto" again after adding new rows to sort, to a new record in the right place; this behavior is similar to the ListView control.
  • In the binding mode or virtual mode (Virtual Mode), if you have to sort the data, then the behavior when inserting data depends on the implementation of the data model. For ADO.NET, new additional lines are automatically sorted to the appropriate position.

Reproduced in: https: //www.cnblogs.com/rainman/p/3661909.html

Guess you like

Origin blog.csdn.net/weixin_33941350/article/details/93561367