1 Attachment(s)
[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:
DataGridView1.AutoResizeRows()
DataGridView1.Refresh()
But the DataGridView doesn't refresh wrap,il looks like this :
http://img266.imageshack.us/img266/5...nshot001cz.jpg
I change manually current selected row moving to another record and now the refresh is done :
http://img525.imageshack.us/img525/5...nshot002az.jpg
How to solve that automatically ?
Also sample attached.
Thanks !
Re: Problem with DatagridView wrapping
still need help,thanks :( !
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.
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:
DataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True
DataGridView1.Columns.Item(0).DefaultCellStyle.WrapMode = DataGridViewTriState.True
Quote:
Originally Posted by
kevininstructor
1 Attachment(s)
Re: Problem with DatagridView wrapping
Quote:
Originally Posted by
mrjohn
Thanks,but not solving for my case,I added this 2 lines in load of form,but the same problem :
vb.net Code:
DataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True
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.
1 Attachment(s)
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 !
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
Re: Problem with DatagridView wrapping
SOLVED !
A BIG THANKS kevininstructor FOR YOUR TIME AND PATIENCE!!!!