使所有行的高度即使在.Net 2.0中的DataGridView中也是如此

本文介绍在.Net2.0中使用AutoSizeRowMode时,如何通过两步操作使DataGridView中所有行的高度保持一致。首先,通过监听RowHeightChanged事件找到最大行高;其次,将AutoSizeRowsMode设为None,并应用最大行高到所有行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文介绍了如何在使用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步

将AutoSizeRowsMo​​de设置为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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值