Question Split a String Value to Multilines within one row in DatagridView

rmclean

Member
Joined
Apr 15, 2010
Messages
6
Programming Experience
1-3
I am trying to find sample code that shows how to split a string value where ever there is a space between the text characters. I need the values to remain within the same cell of a datagridview.
 
If you want the data displayed in a single cell then you need a single value, which would mean rejoining the data after splitting. If you're saying that you want a line break in the data wherever there is currently a space then you have two choices:

1. Replace existing spaces with line breaks:
VB.NET:
str2 = str1.Replace(" ", Environment.NewLine)
2. Split the string into an array on the spaces, then rejoin it using line breaks:
VB.NET:
arr = str1.Split(" "c)
str2 = Array.Join(Environment.NewLine, arr)
 
Thanks for your reply. I'm posting my code where I've tried the first suggestion you made. Although I don't get an error, it seems to replace the spaces in the string with other symbols (screenshot attached). My goal is to have the value displayed like this:

1000
2000
3000
4000

within a single cell. This is a single string where the numbers are separated by spaces.

I want to avoid the display looking like - 1000 2000 3000 4000. In other words, I want the text to appear stacked.

Any suggestions are appreciated.


Private Sub dgvRecords_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgvRecords.CellFormatting

If e.ColumnIndex = 3 Then

Dim strValue As String = TryCast(e.Value, String)
If strValue Is Nothing Then Return

strValue = CStr(dgvRecords.Item(e.ColumnIndex, e.RowIndex).Value)
Dim strArray As String = strValue.Replace(" ", Environment.NewLine)
e.Value = strArray

End If

End Sub
 

Attachments

  • doc1.pdf
    67.6 KB · Views: 28
I answered my own question. I didn't have the word wrap property set. Once I set that, the issue resolved itself :D
 
Back
Top