Windows窗体:单元格包装模式以显示自适应省略号 [英] Windows Forms: Cell Wrapping mode to display adaptive ellipsis

查看:68
本文介绍了Windows窗体:单元格包装模式以显示自适应省略号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Windows Forms Datagridview来显示一些(长)文本。 (代码是PowerShell,但是问题与单元包装模式有关)

  $ TestGridView = New-Object System.Windows .Forms.DataGridView-属性@ {
Name = TestDataGridView
AllowUserToAddRows = $ False
AllowUserToDeleteRows = $ False
位置= 14,225
大小= 1041 ,328
TabIndex = 1
DefaultCellStyle = @ {WrapMode ='True'}
RowHeadersVisible = $ False
AutoSizeColumnsMode ='Fill'
AutoSizeRowsMo​​de ='AllCells'
Anchor ='左,右,上,下'
DefaultCellStyle.Padding =新对象Windows.Forms.Padding -a 2
}

我正在使用Cell Wrapping和AutosizeRowMode,但是我没有找到使DGV单元格显示到特定点的方法,然后在省略时将其截断超过了像元大小。
我想完成的是:(图形编辑)





但到目前为止,我一直无法做到:



WrapMode = False,AutoSizeRowsMo​​de = AllCells



用省略号截断,但删除所有CRLF并仅显示一行





WrapMode = False,AutoSizeRowsMo​​de = None



行高设置为所需值,否则截断与上面相同





WrapMode = True,AutoSizeRowsMo​​de = AllCells



无截断,将显示所有文本和单元格调整高度以适合所有文本





WrapMode = True,AutoSizeRowsMo​​de = None



高度保持应有的状态,但不执行截断。





我要完成的工作是使行的大小最大调整,在此之后,文本应被省略号[...]



我已经尝试过截断内容,但是它具有不利的副作用,即当用户COPY单元格内容,单元格内容会丢失所有截断的部分(当然),因此这不是一个可行的选择。.



非常感谢

解决方案

您需要处理



完整示例



这是一个完整的PowerShell示例。若要查看效果,可以尝试调整列或行的大小。

  Add-Type -AssemblyName System.Windows.Forms 

$ form =新对象System.Windows.Forms.Form
$ form.Add_Load({form_Load -sender $ form -e $ _})

$ dgv =新对象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 =新对象System.Data.DataTable
$ dt.Columns.Add( Column1)
$ dt.Rows.Add( Lorem ipsum dolor sit amet, +`
最合适的fabellas pri,eum aeterno的卷数。)
$ dgv.DataSource = $ dt
#启用多行编辑
$ dgv.Columns [0] .DefaultCellStyle.WrapMode = `
[System.Windows.Forms.DataGridViewTriState] :: True
}

函数dgv_CellPainting {[CmdletBinding()] param(
[parameter()]
[ Object] $ sender,
[parameter()]
[System.Windows.Forms.DataGridViewCellPaintingEventArgs] $ e

#如果不是我们要处理的列则不要处理想要还是它的标题行
if((($ e.ColumnIndex -ne 0)-or($ e.RowIndex -lt 0)){return}

#除了文本以外的所有部分
$ 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}

#绘制文字
[System.Windows.Forms.TextRenderer] :: DrawText($ e.Graphics,$ e.FormattedValue,`
$ e.CellStyle.Font,$ e.CellBoun ds,$ color,`
[System.Windows.Forms.TextFormatFlags] :: VerticalCenter -bor`
[System.Windows.Forms.TextFormatFlags] :: TextBoxControl -bor`
[System .Windows.Forms.TextFormatFlags] :: WordBreak -bor`
[System.Windows.Forms.TextFormatFlags] :: EndEllipsis)

#事件已处理,停止默认处理
$ e.Handled = $ true
}

$ form.ShowDialog()
$ form.Dispose()


I'm using a Windows Forms Datagridview to diplay some (long) text. (The code is PowerShell, but the problem is related to the Cell Wrapping mode)

$TestGridView = New-Object System.Windows.Forms.DataGridView -Property @{
    Name="TestDataGridView"
    AllowUserToAddRows = $False
    AllowUserToDeleteRows = $False
    Location = "14,225"
    Size = "1041,328"
    TabIndex = 1
    DefaultCellStyle= @{WrapMode ='True'}
    RowHeadersVisible=$False
    AutoSizeColumnsMode='Fill'
    AutoSizeRowsMode = 'AllCells' 
    Anchor = 'Left, Right, Top, Bottom'
    DefaultCellStyle.Padding =  new-object Windows.Forms.Padding -a 2
}

I'm using Cell Wrapping and AutosizeRowMode, but I have found no way to have a DGV cell display up to a certain point, then truncate by ellipsis when the cell size is exceeded. What I'd want to accomplish is this: (graphic edit)

but so far, I've been unable to do so:

WrapMode=False,AutoSizeRowsMode=AllCells

truncates by ellipsis, but removes all CRLFs and displays just one line

WrapMode=False,AutoSizeRowsMode=None

Row height set to desired value, but otherwise truncation same as above

WrapMode=True,AutoSizeRowsMode=AllCells

No truncation, all the text is displayed and the cell is adapted in height to fit all the text

WrapMode=True,AutoSizeRowsMode=None

Height stays as it shoulds, but no truncation is performed.

What I'm trying to accomplish is to have the rows adjust in size up to a maximum, after which text should be truncated by ellipsis [...]

I'have already tried truncating the content, but it has the adverse side effect that when the user COPY the cell content, the cell content is missing all the truncated part (of course) so it is not a viable option..

Many thanks

解决方案

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()

这篇关于Windows窗体:单元格包装模式以显示自适应省略号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆