Results 1 to 5 of 5

Thread: [RESOLVED] Adding a new line into a cell of a DataGridView

  1. #1

    Thread Starter
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Resolved [RESOLVED] Adding a new line into a cell of a DataGridView

    Hello.

    I have searched for this, and not been able to find any examples where someone has done this. I'm trying to add a new line into the cell of a DataGridView so the text appears on 2 lines (of the same cell). Here's some code to explain what i'm doing:

    Code:
    Dim DS As New DataSet("aa")
    Dim tbl As DataTable = DS.Tables.Add("bb")
    Dim xRow2 As DataRow = tbl.NewRow()
    xRow2("Equipment") = "-Lawnmower" & vbCrLf & "-Logcutter"
    tbl.Rows.Add(xRow2)
    grdRented.DataSource = DS.Tables("bb")
    But the entries keep showing up on the same line with an unknown character code (ie: -Lawnmower[]-Logcutter) instead of being on two separate lines. How can i make it bump the text down a line (inside the cell) so that it appears like this?:

    -Lawnmower
    -Logcutter

    I've seen this code done (and work) in VB6 with a grid control, so i'm confused as to why i can't get it to work now. Am i missing some super top secret setting that allows it to span two lines?
    .
    Last edited by MrGTI; Oct 11th, 2011 at 09:41 AM. Reason: resolved
    ~Peter


  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: Adding a new line into a cell of a DataGridView

    See attached VS2008 project where the second column does wrapping. Some of the properties are set in the DataGridView property window and one is set in the last line of the Load event of the form.
    Attached Files Attached Files

  3. #3
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: Adding a new line into a cell of a DataGridView

    In my first reply I added rows directly to the DataGridView. Using the code below shows that there is no difference when using a DataTable.

    Code:
        Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim dt As New DataTable
    
            dt.Columns.AddRange(New DataColumn() _
               {New DataColumn("Identifier", GetType(System.Int32)), _
                New DataColumn("Field1", GetType(System.String))})
    
            dt.Rows.Add(New Object() {1, "Lorem ipsum dolor sit amet," & Environment.NewLine & "consectetuer adipiscing elit," & Environment.NewLine & "sed diam nonummy nibh euismod"})
            dt.Rows.Add(New Object() {2, "Lorem ipsum dolor sit amet, consectetuer adipiscing elit"})
            dt.Rows.Add(New Object() {3, "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod"})
            dt.Rows.Add(New Object() {4, "Line 1" & Environment.NewLine & "Line 2" & Environment.NewLine & "Last line"})
    
            DataGridView1.DataSource = dt
    
            DataGridView1.Columns.Item("Column2").DefaultCellStyle.WrapMode = DataGridViewTriState.True
        End Sub
    Set Column1 DataProperty to Identifier and Column2 to Field1

  4. #4

    Thread Starter
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Thumbs up Re: Adding a new line into a cell of a DataGridView

    The problem wasn't the use of vbCrLf vs your Environment.NewLine. It actually came down to a line line of code that you mentioned.

    Code:
    grdRented.DefaultCellStyle.WrapMode = DataGridViewTriState.True
    All i needed to do was to turn on the ability for the DataGridView to allow text to wrap. Once i added that, it worked. Thanks for your help!
    .
    ~Peter


  5. #5
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: Adding a new line into a cell of a DataGridView

    Quote Originally Posted by MrGTI View Post
    The problem wasn't the use of vbCrLf vs your Environment.NewLine. It actually came down to a line line of code that you mentioned.

    Code:
    grdRented.DefaultCellStyle.WrapMode = DataGridViewTriState.True
    All i needed to do was to turn on the ability for the DataGridView to allow text to wrap. Once i added that, it worked. Thanks for your help!
    .
    Good to hear this worked. In regards to vbCrLf vs Environment.NewLine, NewLine is a constant customized specifically for the current platform and implementation of the .NET Framework as per MSDN page so if you are fine with vbCrLf so be it. Indeed DefaultCellStyle.WrapMode = DataGridViewTriState.True was what you needed :-)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width