none
How to Wrap text in a text box column of datagrid in windows application.

    Question

  • I have a datagrid column in which I want to wrap the text in one column. 

    private DataGridTextBoxColumn dgcComments
    dgcComments = new DataGridTextBoxColumn()
    dgcComments.TextBox.Multiline = true
    dgcComments.TextBox.AcceptsReturn = true
    dgcComments.TextBox.WordWrap = true

    I have set the WordWrap property to True and Multiline property to True, but it doesn't seem to work.  I am able to see the wraped text only when I manually increase the size of the row and click in the cell containing value.

    Anybody has any idea how to wrap text in a text box column of datagrid?

    Regards

    Hemant

    Monday, January 22, 2007 12:33 PM

All replies

  • You have to create a custom column style for that.  You will find an example on the VB-Tips website.
    Monday, January 22, 2007 5:45 PM
  • Maybe this thread will help.
    Monday, January 22, 2007 8:02 PM
  •  

    If you set the datagridview's AutoSizeRowsMode to AllCells and the columns WrapMode to DatagridViewTriState.True the rows will autosize and wrap the text.  The row will not resize while the user is typing it will adjust its height after the user moves to a new cell.

     



            Dim dt As New DataTable
            Dim strConn As String = _
               "Server = .\SQLEXPRESS;Database = NorthWind; Integrated Security = SSPI;"
            Dim conn As New SqlConnection(strConn)
            Dim da As New SqlDataAdapter("Select LastName, Notes from Employees", conn)
            da.Fill(dt)
            DataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
            DataGridView1.DataSource = dt
            With DataGridView1.Columns("Notes")
                .Width = 150
                .AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
                .DefaultCellStyle.WrapMode = DataGridViewTriState.True
            End With

    here is an example code by ken,I think it is useful
    Tuesday, January 23, 2007 2:07 AM
  • Private Sub OnAddCols(ByVal sender As Object, ByVal e As EventArgs) Handles m_AddColsToGridButton.Click

            m_Grid.Columns.Add("MyColumnName", "MyColumnHeaderText")

            With m_Grid.Columns(0)

                .Width = 150

                .AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells

                .DefaultCellStyle.WrapMode = DataGridViewTriState.True

            End With

        End Sub

     

    here is an example of a button-click,it can add an row which can wrap text.

    Tuesday, January 23, 2007 2:39 AM
  • Hi

    I could not find DataGridView  in .Net 2003.

    Hemant

    Tuesday, January 23, 2007 9:00 AM
  • Hi

    I found the code on a given link but the code is incomplete as there is no class 'HotTrackTextBoxColumn'


    Regards

    Hemant

    Tuesday, January 23, 2007 9:19 AM
  • datagridview is a upgrade control of datagrid in vs2005.

    If you want to use datagrid in 2003, Probably u can do this, but even then i feel that this is not sufficient, since when the word is getting wrapped, the row height will not be increased.
    example

    Dim grdCol1 As New DataGridTextBoxColumn
    With grdCol1
    .MappingName = "MENUNAME"
    .HeaderText = "Menu Name"
    .Width = 40
    .ReadOnly = False
    .TextBox.WordWrap = True ' word wrap property
    .TextBox.Multiline = True ' multi line property
    End With

    ---------------------------
    May be this code will help u.

    Courtesy by: Ken Tucker [MVP] (VIP)

    Imports System.Reflection

    Public Class MultiLineColumn

    Inherits DataGridTextBoxColumn

    Private mTxtAlign As HorizontalAlignment

    Private mDrawTxt As New StringFormat

    Private mbAdjustHeight As Boolean = True

    Private m_intPreEditHeight As Integer

    Private m_rownum As Integer

    Dim WithEvents dg As DataGrid

    Private arHeights As ArrayList

    Private Sub GetHeightList()

    Dim mi As MethodInfo = dg.GetType().GetMethod("get_DataGridRows",
    BindingFlags.FlattenHierarchy Or BindingFlags.IgnoreCase Or
    BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.Public Or
    BindingFlags.Static)

    Dim dgra As Array = CType(mi.Invoke(Me.dg, Nothing), Array)

    arHeights = New ArrayList

    Dim dgRowHeight As Object

    For Each dgRowHeight In dgra

    If dgRowHeight.ToString().EndsWith("DataGridRelationshipRow") = True Then

    arHeights.Add(dgRowHeight)

    End If

    Next

    End Sub

    Public Sub New()

    mTxtAlign = HorizontalAlignment.Left

    mDrawTxt.Alignment = StringAlignment.Near

    Me.ReadOnly = True

    End Sub

    Protected Overloads Overrides Sub Edit(ByVal source As
    System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds
    As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal instantText
    As String, ByVal cellIsVisible As Boolean)

    MyBase.Edit(source, rowNum, bounds, [readOnly], instantText, cellIsVisible)

    Me.TextBox.TextAlign = mTxtAlign

    Me.TextBox.Multiline = mbAdjustHeight

    If rowNum = source.Count - 1 Then

    GetHeightList()

    End If

    End Sub

    Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing.Graphics,
    ByVal bounds As System.Drawing.Rectangle, ByVal source As
    System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal
    backBrush As System.Drawing.Brush, ByVal foreBrush As System.Drawing.Brush,
    ByVal alignToRight As Boolean)

    Static bPainted As Boolean = False

    If Not bPainted Then

    dg = Me.DataGridTableStyle.DataGrid

    GetHeightList()

    End If

    'clear the cell

    g.FillRectangle(backBrush, bounds)

    'draw the value

    Dim s As String = Me.GetColumnValueAtRow([source], rowNum).ToString()

    Dim r As New RectangleF(bounds.X, bounds.Y, bounds.Width, bounds.Height)

    r.Inflate(0, -1)

    ' get the height column should be

    Dim sDraw As SizeF = g.MeasureString(s, Me.TextBox.Font, Me.Width, mDrawTxt)

    Dim h As Integer = sDraw.Height + 15

    If mbAdjustHeight Then

    Try

    Dim pi As PropertyInfo = arHeights(rowNum).GetType().GetProperty("Height")

    ' get current height

    Dim curHeight As Integer = pi.GetValue(arHeights(rowNum), Nothing)

    ' adjust height

    If h > curHeight Then

    pi.SetValue(arHeights(rowNum), h, Nothing)

    Dim sz As Size = dg.Size

    dg.Size = New Size(sz.Width - 1, sz.Height - 1)

    dg.Size = sz

    End If

    Catch

    ' something wrong leave default height

    GetHeightList()

    End Try

    End If

    g.DrawString(s, MyBase.TextBox.Font, foreBrush, r, mDrawTxt)

    bPainted = True

    End Sub

    Public Property DataAlignment() As HorizontalAlignment

    Get

    Return mTxtAlign

    End Get

    Set(ByVal Value As HorizontalAlignment)

    mTxtAlign = Value

    If mTxtAlign = HorizontalAlignment.Center Then

    mDrawTxt.Alignment = StringAlignment.Center

    ElseIf mTxtAlign = HorizontalAlignment.Right Then

    mDrawTxt.Alignment = StringAlignment.Far

    Else

    mDrawTxt.Alignment = StringAlignment.Near

    End If

    End Set

    End Property

    Public Property AutoAdjustHeight() As Boolean

    Get

    Return mbAdjustHeight

    End Get

    Set(ByVal Value As Boolean)

    mbAdjustHeight = Value

    Try

    dg.Invalidate()

    Catch

    End Try

    End Set

    End Property

    End Class

    --
    Courtesy by: Ken Tucker [MVP] (VIP)

    Tuesday, January 23, 2007 10:15 AM
  • The Hot Track Column is a textbox column that changes color when the mouse is over the cell.  You can have the Mulitline column inherit from the DataGridTextBoxColumn.  Here is a link to the Hot Track Column in case you want to use that.  

    Tuesday, January 23, 2007 12:03 PM
  • Hi

    Any idea how we can increase the row height once the word gets wraped.

    Regards

    Hemant

    Wednesday, January 24, 2007 2:55 PM
  • Hi I found one article from FAQ:

    http://www.windowsforms.net/FAQs/default.aspx?PageID=2&ItemID=694&CategoryID=3&tabindex=3

    so for c# just add

    this.dataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
    I heve test it , works well.
    and for vb, it may be Me.dataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True
    Monday, January 29, 2007 7:21 AM
  • The code sample will adjust the height when the user finishes editing the cell
    Monday, January 29, 2007 10:34 AM
  • Hi

    DataGridView control is in .Net  2005.  I am searching for a solution in .Net 2003

    Regards

    Hemant

     

     

    Tuesday, February 13, 2007 4:54 PM