I recently updated my code from using Rows.Add
in a foreach loop to using AddRange
with populated list of rows
. This has proven to increase the speed of populating the datagridview (significantly), but I'm having issues with the row height. This was not an issue with Rows.Add
as I could easily just set the rowtemplate.height
property. Parts of the event:
var items = listItems; int count = 1;
object[] buffer = new object[3];
List<DataGridViewRow> rows = new List<DataGridViewRow>();
foreach (var i in items)
{
buffer[0] = count;
buffer[1] = i.Desc;
buffer[2] = i.ID;
rows.Add(new DataGridViewRow());
rows[rows.Count - 1].CreateCells(datagridItems, buffer);
count ;
}
datagridItems.Rows.AddRange(rows.ToArray());
For whatever reason, whatever the rowtemplate.height
property is set to, it doesn't matter when adding rows this way. I'm pretty sure it has to do with this rows.Add(new DataGridViewRow());
, because if I try to add this just before the AddRange
:
foreach (DataGridViewRow r in rows)
{
r.MinimumHeight = 46;
r.Height = 46;
}
^ it works. However I don't think this a good way of doing it. So I've tried to play around with the new DataGridViewRow()
, but with no success thus far:
var items = listItems; int count = 1;
object[] buffer = new object[3];
List<DataGridViewRow> rows = new List<DataGridViewRow>();
foreach (var i in items)
{
buffer[0] = count;
buffer[1] = img;
buffer[2] = i.ID;
//rows.Add(new DataGridViewRow()); From the old example
rows.Add(new DataGridViewRow
{
MinimumHeight = 46,
Height = 46
});
rows[rows.Count - 1].CreateCells(datagridItems, buffer);
count ;
}
/* this works, but there should be a much better way of doing it
foreach (DataGridViewRow r in rows)
{
r.MinimumHeight = 46;
r.Height = 46
}
*/
datagridItems.Rows.AddRange(rows.ToArray());
And for the record, these are some of the (maybe) relevant properties set on the datagridview
//by the way, the datagridview is added in the designer
datagridItems.RowTemplate.Height = 46;
datagridItems.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
datagridItems.CellBorderStyle = DataGridViewCellBorderStyle.None;
datagridItems.RowTemplate.MinimumHeight = 46;
CodePudding user response:
The DataGridView.RowTemplate is useful for cases that you do not explicitly add a DataGridViewRow
and you just set data source or pass the values or pass the row count. In your case, since you are creating the rows yourself, it's your responsibility to configure the row's properties manually, or create the rows by cloning the template manually.
In all the following examples RowTemplate
property will be automatically used to create the row:
dataGridView1.DataSource = myList;
dataGridView1.RowCount = 5;
dataGridView1.Rows.Add(3);
dataGridView1.Rows.Add(new object[]{1, "One"});
dataGridView1.Rows.Insert(0, new object[]{1, "One"});
If you add a row like this dataGridView1.Rows.Add(myNewRow)
, you have to take care of the myNewRow
properties yourself, for example:\
var myNewRow = new DataGridViewRow() {Height = 50};
Or you can create myNewRow
by cloning the RowTemplate
, for example:
var myNewRow = (DataGridViewRow)dataGridView1.RowTemplate.Clone();
You may learn more by tracing the usages, in the source code of the RowTemplate
property.