本文介绍了如何在使用AutoSizeRowMode的同时在.Net 2.0中的DataGridView中使所有行的高度相同
在DataGridView 2.0中,有一个选项可以调整行的大小以适合单元格的内容。这将增加行的高度。
this.dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells;
但是整个DataGridView的行高都不一样(某些行的高度小于另一行的height)
要使DataGridView中的所有行都显示为相同大小,请执行以下步骤来设置最大行高并将其设置为dataGridView中所有行的高度:
步骤1:找出最大行高并将其高度设置为DataGridView首选rowheight
void dataGridView1_RowHeightChanged(object sender, DataGridViewRowEventArgs e)
{
int rowHeight = e.Row.Height;
if (this.dataGridView1.RowTemplate.Height < rowHeight)
{
this.dataGridView1.RowTemplate.Height = rowHeight;
}
}
第2步
将AutoSizeRowsMode设置为none,然后将最大行高应用于所有行
private void dgLocation_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
int rowHeight=this.dataGridView1.RowTemplate.Height;
this.dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
int numRows = this.dataGridView1.Rows.Count;
for (int i = 0; i < numRows; i++)
{
this.dataGridView1.Rows[i].Height = rowHeight;
}
}
From: https://bytes.com/topic/net/insights/658760-make-all-rows-height-even-datagridview-net-2-0-a