Results 1 to 8 of 8

Thread: [RESOLVED] Problem with DatagridView wrapping

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    216

    Resolved [RESOLVED] Problem with DatagridView wrapping

    Hi,
    I have a DataGridView with one column,I set for that column DefaultcelStyle->Wrapmode=True

    Modify data a with a long string,I call also
    vb.net Code:
    1. DataGridView1.AutoResizeRows()
    2.         DataGridView1.Refresh()

    But the DataGridView doesn't refresh wrap,il looks like this :


    I change manually current selected row moving to another record and now the refresh is done :


    How to solve that automatically ?
    Also sample attached.
    Thanks !
    Attached Files Attached Files

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    216

    Re: Problem with DatagridView wrapping

    still need help,thanks !

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

    Re: Problem with DatagridView wrapping

    A solution is here with an attached working project http://www.vbforums.com/showthread.p...09#post4076109

    Make sure to read the comments in the project.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    216

    Re: Problem with DatagridView wrapping

    Thanks,but not solving for my case,I added this 2 lines in load of form,but the same problem :
    vb.net Code:
    1. DataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True
    2.         DataGridView1.Columns.Item(0).DefaultCellStyle.WrapMode = DataGridViewTriState.True


    Quote Originally Posted by kevininstructor View Post
    A solution is here with an attached working project http://www.vbforums.com/showthread.p...09#post4076109

    Make sure to read the comments in the project.

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

    Re: Problem with DatagridView wrapping

    Quote Originally Posted by mrjohn View Post
    Thanks,but not solving for my case,I added this 2 lines in load of form,but the same problem :
    vb.net Code:
    1. DataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True
    2.         DataGridView1.Columns.Item(0).DefaultCellStyle.WrapMode = DataGridViewTriState.True
    Then you are doing something incorrect from what I gave you and indicated to read the instructions.

    Example, I took the example given to you and replaced the static lines of filling the DataTable rows with a dynamic method of reading a Text File and it worked just fine as suspected.

    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))})
    
        Dim Request = (From line In IO.File.ReadAllLines("Document.txt") _
                       Where line.Length > 0 _
                       Let Items = line.Split(","c) _
                       Select New With _
                            {.ID = Items(0), _
                             .Text = Items(1) _
                            } _
                        ).ToList
    
        For Each line In Request
            dt.Rows.Add(New Object() {line.ID, line.Text})
        Next
        DataGridView1.DataSource = dt
        DataGridView1.Columns.Item("Column2").DefaultCellStyle.WrapMode = DataGridViewTriState.True
    End Sub

    Contents of Document text which has line breaks is attached.
    Attached Files Attached Files

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    216

    Re: Problem with DatagridView wrapping

    Thanks kevininstructor for your answer. The problem is when I update data grid, I first run with short string data (no wrap need) then I modify data to need wrap,but the grid doesn't do that automatically.
    I've attached again the sample,this time I used your txt file and code,I just create a second string column and put there first 10 caracter from column 1 in order to have initial no wrapping needed.
    Run the sample and press Modify data button,there I put column 1 (long) into 2 (short text) but the grid doesn't refresh automatically,no wrap is done

    Thanks !
    Attached Files Attached Files

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

    Re: Problem with DatagridView wrapping

    Changing properties of the DataGridView will not help in this case, instead you need to work at the DataSource level, the DataTable then tell the BindingSource as shown below.

    Code to make this work is marked in red. Note I took some of your code out.

    Code:
    Public Class Form1
        Dim dt As New DataTable
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            dt.Columns.AddRange(New DataColumn() _
               {New DataColumn("Identifier", GetType(System.Int32)), _
                New DataColumn("Field1", GetType(System.String)), New DataColumn("Field2", GetType(System.String))})
    
            Dim Request = (From line In IO.File.ReadAllLines("Document.txt") _
                           Where line.Length > 0 _
                           Let Items = line.Split(","c) _
                           Select New With _
                                {.ID = Items(0), _
                                 .Text = Items(1) _
                                } _
                            ).ToList
    
            For Each line In Request
                dt.Rows.Add(New Object() {line.ID, line.Text, line.Text.Substring(0, 10)})
            Next
            dt.AcceptChanges()
    
            BindingSource1.DataSource = dt
    
            DataGridView1.Columns(0).DefaultCellStyle.WrapMode = DataGridViewTriState.True
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            DirectCast(BindingSource1.Current, DataRowView)("Field2") = dt.Rows(0)("Field1").ToString
            DirectCast(BindingSource1.DataSource, DataTable).AcceptChanges()
            BindingSource1.ResetCurrentItem()
        End Sub
    End Class

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    216

    Re: Problem with DatagridView wrapping

    SOLVED !
    A BIG THANKS kevininstructor FOR YOUR TIME AND PATIENCE!!!!

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