wordwrap不适用于VB.net中的datagridtextboxcolumn

时间:2014-11-30 02:30:28

标签: vb.net

我在这个博客上看过以前的答案似乎对我没什么帮助。

我有一个datagrid并添加了datagridtextboxcolumn的列。当我单击datagrid上的一个单元格时 - 如果该行对于单元格的宽度来说太大,它将显示在下一行,但是当我没有单击该单元格时,文本行的末尾将不会显示在下一行 - 因此不显示。

我从datagridtextboxcolumn派生了一个新类,并试图覆盖paint和painttext方法,这似乎没有效果 - 文本仍然显示在列中,只显示一行。

我的代码是

这是派生类: -

(下面我已经覆盖了paint和painttext类,以便查看哪个方法对数据网格中的显示有一些影响 - 但是这个覆盖过程没有任何影响。

enter code here

Imports Microsoft.VisualBasic
Imports System.ComponentModel
Imports System.Data
Imports System.Data.Common
Imports System.Data.OleDb
Imports System.Drawing
Imports System.Windows.Forms
Namespace DataGridRichTextBox


Public Class DataGridRichTextBoxColumn
    Inherits DataGridTextBoxColumn

    Private _source As CurrencyManager
    Private _rowNum As Integer
    Private _isEditing As Boolean



    Public Sub New()
        _source = Nothing
        _isEditing = False
    End Sub 'New



    Protected Overloads Sub PaintText(ByRef g As Graphics, ByVal bounds As System.Drawing.Rectangle, ByRef Text As String, ByVal alligntoright As Boolean)

    End Sub

    Protected Overloads Sub PaintText(ByRef g As Graphics, ByVal bounds As System.Drawing.Rectangle, ByRef Text As String, ByRef s1 As System.Drawing.Brush, ByRef s2 As System.Drawing.Brush, ByVal alligntoright As Boolean)
    End Sub

    Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal _source As CurrencyManager, ByVal num As Integer)
    End Sub

    Protected Overloads Overrides Sub SetColumnValueatrow(ByVal _source As CurrencyManager, ByVal num As Integer, ByVal obj As Object)
    End Sub

End Class 'DataGridComboBoxColumn

结束命名空间

'这是我将派生类作为对象添加到datagrid的地方: -

Imports System.Windows.Forms
Imports System.Data.SqlClient
Imports System.Drawing




Public Class DataGridMine
    Inherits DataGrid



Public r_counter, column_num, x1 As Integer

Public x13 As Integer

#Region " Windows Form Designer generated code "


Public Sub New()
    MyBase.New()

    'This call is required by the Windows Form Designer.
    InitializeComponent()

 End Sub
'Form overrides dispose to clean up the component list.
 Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
        If Not (components Is Nothing) Then
            components.Dispose()
        End If
    End If
    MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
Friend WithEvents SqlSelectCommand2 As New SqlCommand
Friend WithEvents DataSet51 As New tasks_trial2.DataSet5
Public WithEvents DataGridTableStyle1 As New DataGridTableStyle



Public WithEvents task_name_col, parent_col As New DataGridTextBoxColumn


Public WithEvents description_col As New DataGridRichTextBox.DataGridRichTextBoxColumn



Friend WithEvents SqlDataAdapter3 As New SqlDataAdapter

Friend WithEvents SqlDataAdapter2 As New SqlDataAdapter
Friend WithEvents SqlSelectCommand3 As New SqlCommand




   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
    CType(Priority_code_table1, System.ComponentModel.ISupportInitialize).BeginInit()



    SuspendLayout()



    Me.DataMember = "tasks"
    Me.DataSource = DataSet51
    Me.Location = New Point(8, 230)
    Me.Size = New Size(1117, 384)
    Me.TabIndex = 0


    Me.TableStyles.AddRange(New DataGridTableStyle() {DataGridTableStyle1})





    SqlDataAdapter2.SelectCommand = SqlSelectCommand2
    SqlDataAdapter2.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "tasks")})

    SqlSelectCommand2.CommandText = ""
    SqlSelectCommand2.Connection = SqlConnection10


    DataGridTableStyle1.DataGrid = Me



    DataGridTableStyle1.AllowSorting = False

    column_num = 3

    DataGridTableStyle1.HeaderForeColor = SystemColors.ControlText
    DataGridTableStyle1.MappingName = "tasks"
    DataGridTableStyle1.SelectionBackColor = Color.Aquamarine
    DataGridTableStyle1.SelectionForeColor = Color.Black
    DataGridTableStyle1.PreferredRowHeight = 10
    DataGridTableStyle1.PreferredColumnWidth = 75



    description_col.HeaderText = "Description"
    description_col.MappingName = "description"
    description_col.Width = 260

” .....................

 ' where column is description_col.


  Public Sub add_columns(ByRef dgrid1 As DataGridMine, ByVal column As Object)

        dgrid1.DataGridTableStyle1.GridColumnStyles.AddRange(New DataGridColumnStyle() {column})

    End Sub

1 个答案:

答案 0 :(得分:0)

如果您使用DataGridViewTextBoxColumn

,则不必执行所有操作

只需通过代码或属性菜单设置一些选项

转到您的列列表,然后选择您希望文本被包裹起来但不会被剪掉的列。

转到DefaulCellStyle并设置

WrapMode = True

另外你可以在DGV属性菜单上设置它

AutoSizeColumnsMode to Fill

AutoSizeRowsMode to AllCells

应该做的伎俩

这一切都在“属性菜单”中设置,但您也可以在“代码”中进行此选择