简体   繁体   中英

Inserting an empty row in DataGridView

I want to introduce a graphical separation between group of rows in a DataGridView.

Which are the options I have: - Should I introduce an empty row? - Should I work with borders and/or the paint methods ?

This increases the lower border of the row at specified [Index]:

DataGridViewRow row = dataGridView1.Rows[Index];
row.DividerHeight = 1;

Note that DividerHeigth uses the space of the row, so if you set it to 10 it can cover half row (for me, 1 is enough).

There is also DividerWidth property to separate groups of columns.

grid.Rows.Insert(index, 1);
var addedRow = grid.Rows[index];

This inserts 1 empty templated row at 'index'.

With the Rows.Add() method you add a new row, you can obtain a reference to it by using:

var newRow = dg.Rows[dg.Rows.Add()];

So you can manipulate your new row after, example:

newRow.Cells["myColumn"].Value = "asd";
DataGridViewRow DGVR= (DataGridViewRow)yourDataGridView.Rows[0].Clone();
DGVR.Cells[0].Value = "XYZ";
DGVR.Cells[1].Value = 50.2;
yourDataGridView.Rows.Add(DGVR);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM