简体   繁体   English

VB.net 中 Datagridview 的自动调整行不工作

[英]Autosize Rows for Datagridview in VB.net not working

Hi, i'm making a memo type of data grid with Dynamic data from Database and somehow i cannot make the rows auto resize.嗨,我正在使用数据库中的动态数据制作备忘录类型的数据网格,但不知何故我无法使行自动调整大小。 i tried pretty much everything, here's some snippet of my code which i put on Form Load sub:我几乎尝试了一切,这是我放在 Form Load sub 上的一些代码片段:

    DataGridView2.Columns(0).AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
    DataGridView2.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
    DataGridView2.Columns(2).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
    DataGridView2.Columns(2).DefaultCellStyle.WrapMode = DataGridViewTriState.True
    DataGridView2.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders

Which came out like this:结果是这样的:

在此处输入图像描述

After That i tried using this on DataGridView2.CellPainting Event之后我尝试在 DataGridView2.CellPainting 事件上使用它

    If e.Value Is Nothing Then Return
    Dim s = e.Graphics.MeasureString(e.Value.ToString(), DataGridView2.Font)

    If s.Width > DataGridView2.Columns(e.ColumnIndex).Width Then

        Using gridBrush As Brush = New SolidBrush(DataGridView2.GridColor), backColorBrush As Brush = New SolidBrush(e.CellStyle.BackColor)

            Using gridLinePen As Pen = New Pen(gridBrush)
                e.Graphics.FillRectangle(backColorBrush, e.CellBounds)
                e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1)
                e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1)
                e.Graphics.DrawString(e.Value.ToString(), DataGridView2.Font, Brushes.Black, e.CellBounds, StringFormat.GenericDefault)
                DataGridView2.Rows(e.RowIndex).Height = CInt((s.Height * Math.Ceiling(s.Width / DataGridView2.Columns(e.ColumnIndex).Width)))
                e.Handled = True
            End Using
        End Using
    End If

which came out like this:结果是这样的:

在此处输入图像描述

Tried fiddling with all properties but i seems unable to figure it out, Thank you in advance尝试摆弄所有属性,但我似乎无法弄清楚,提前谢谢你

Here's the process I followed:这是我遵循的过程:

  • Dropped a new datagridview on a form (actually I dragged it out of the datasources window, because I already had a dataset in the project)在表单上放置了一个新的datagridview(实际上我将它拖出数据源window,因为我已经在项目中有一个数据集)

在此处输入图像描述

  • Chose edit columns, chose a column, clicked [...] next to DefaultCellStyle for one of the string columns, set WrapMode to True选择编辑列,选择一列,单击其中一个字符串列的 DefaultCellStyle 旁边的[...] ,将 WrapMode 设置为 True

在此处输入图像描述

  • Set AutoSizeRowsMode of the DGV to AllCells将 DGV 的AutoSizeRowsMode de 设置为 AllCells

在此处输入图像描述

  • Inserted a long string into the underlying datatable the dgv was bound to:在 dgv 绑定到的基础数据表中插入了一个长字符串:

在此处输入图像描述

  • And the result wrapped:结果包装:

在此处输入图像描述

You can consider centering the elements in the DataGridView:可以考虑将DataGridView中的元素居中:

    DataGridView2.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells
    DataGridView2.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter

Result looks like:结果如下:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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