Windows Forms: Cell Wrapping mode to display adaptive ellipsis

孤街醉人 提交于 2019-12-07 03:54:27

You need to handle CellPainting event yourself and draw text yourself by applying word-wrapping and ellipsis:

Function dgv_CellPainting{[CmdletBinding()]param( 
    [parameter()] 
    [Object]$sender, 
    [parameter()] 
    [System.Windows.Forms.DataGridViewCellPaintingEventArgs]$e 
) 
    #Don't process if it's not the column which we want or it's a header row
    if (($e.ColumnIndex -ne 0) -or ($e.RowIndex -lt 0)){ return }

    #Paint all parts but text        
    $e.Paint($e.CellBounds, [System.Windows.Forms.DataGridViewPaintParts]::All `
        -band (-bnot([System.Windows.Forms.DataGridViewPaintParts]::ContentForeground)))
    $color = $e.CellStyle.ForeColor
    if ($sender.Rows[$e.RowIndex].Cells[$e.ColumnIndex].Selected -eq $true){
        $color = $e.CellStyle.SelectionForeColor}

    #Paint text
    [System.Windows.Forms.TextRenderer]::DrawText($e.Graphics, $e.FormattedValue, `
        $e.CellStyle.Font, $e.CellBounds, $color, `
                [System.Windows.Forms.TextFormatFlags]::VerticalCenter -bor `
                [System.Windows.Forms.TextFormatFlags]::TextBoxControl -bor `
                [System.Windows.Forms.TextFormatFlags]::WordBreak -bor `
                [System.Windows.Forms.TextFormatFlags]::EndEllipsis)

    #Event handled, stop default processing
    $e.Handled = $true
}

Full Example

Here is a full working PowerShell example. To see the effect, you can try to resize the column or row.

Add-Type -AssemblyName System.Windows.Forms

$form = New-Object System.Windows.Forms.Form
$form.Add_Load({form_Load -sender $form -e $_})

$dgv = New-Object System.Windows.Forms.DataGridView
$dgv.Dock = [System.Windows.Forms.DockStyle]::Fill
$dgv.RowTemplate.Height = 50
$dgv.Add_CellPainting({dgv_CellPainting -sender $dgv -e $_})

$form.Controls.Add($dgv)

Function form_Load {[CmdletBinding()]param( 
    [parameter()] 
    [Object]$sender, 
    [parameter()] 
    [System.EventArgs]$e 
) 
    $dt = New-Object System.Data.DataTable
    $dt.Columns.Add("Column1")
    $dt.Rows.Add("Lorem ipsum dolor sit amet, " + `
        "wisi fierent fabellas pri et, eum aeterno volumus no.")
    $dgv.DataSource = $dt
    #Enable multiline editing
    $dgv.Columns[0].DefaultCellStyle.WrapMode = `
        [System.Windows.Forms.DataGridViewTriState]::True
}

Function dgv_CellPainting{[CmdletBinding()]param( 
    [parameter()] 
    [Object]$sender, 
    [parameter()] 
    [System.Windows.Forms.DataGridViewCellPaintingEventArgs]$e 
) 
    #Don't process if it's not the column which we want or it's a header row
    if (($e.ColumnIndex -ne 0) -or ($e.RowIndex -lt 0)){ return }

    #Paint all parts but text        
    $e.Paint($e.CellBounds, [System.Windows.Forms.DataGridViewPaintParts]::All `
        -band (-bnot([System.Windows.Forms.DataGridViewPaintParts]::ContentForeground)))
    $color = $e.CellStyle.ForeColor
    if ($sender.Rows[$e.RowIndex].Cells[$e.ColumnIndex].Selected -eq $true){
        $color = $e.CellStyle.SelectionForeColor}

    #Paint text
    [System.Windows.Forms.TextRenderer]::DrawText($e.Graphics, $e.FormattedValue, `
        $e.CellStyle.Font, $e.CellBounds, $color, `
                [System.Windows.Forms.TextFormatFlags]::VerticalCenter -bor `
                [System.Windows.Forms.TextFormatFlags]::TextBoxControl -bor `
                [System.Windows.Forms.TextFormatFlags]::WordBreak -bor `
                [System.Windows.Forms.TextFormatFlags]::EndEllipsis)

    #Event handled, stop default processing
    $e.Handled = $true
}

$form.ShowDialog()
$form.Dispose()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!