Reputation: 41
I have a string like so:
"{[Entry Number: 1 | @AppUser: James | @AppTimeDate: 5/21/2018 | @AppText: A lot of multiline text here]}{[Entry Number: 2 | @AppUser: John | @AppTimeDate: 5/22/2018 | @AppText: More multiline text here]}"
I want to get this string into a DataGridView like so:
------------------------------ |Name |Text | -This is the header ------------------------------ |James |A lot of multiline| -This is all one row for James |5/21/2018|text here | ------------------------------ |John |More multiline | -This is all another row for John |5/21/2018|text here | ------------------------------
Can anyone provide me an example of how this would be accomplished? I did a lot of googling, but I can't seem to find an example of how I would accomplish something like this.
I've tried doing this so far, but I keep getting a "System.Reflection.TargetInvocationException" error:
Dim RawNote As String = txtNotes.Text
Dim Splitter As String = "{[Entry Number: "
Dim substrings() As String = Regex.Split(RawNote, Splitter)
For Each match As String In substrings
MsgBox(match)
Next
Upvotes: 2
Views: 259
Reputation: 32223
Using Linq, take these steps:
Entries
Fields
)@AppText
, @AppTimeDate
etc.)Note:
This code assumes there's a DataGridView
already setup with two text columns.
Dim RawNote As String = "{[Entry Number: 1 | @AppUser: James | @AppTimeDate: 5/21/2018 | @AppText: A lot of multiline text here]}" &
"{[Entry Number: 2 | @AppUser: John | @AppTimeDate: 5/22/2018 | @AppText: More multiline text here]}"
Dim InputList As List(Of String()) =
RawNote.Split(New String() {"]}{["}, StringSplitOptions.None).
Select(Function(part) part.Replace("{[", "").Replace("]}", "")).
Select(Function(elm) elm.Split("|"c).
Select(Function(str) str.Remove(0, str.IndexOf(":"c) + 1)).
Skip(1).ToArray()).
Select(Function(f) New String() {f(0) + Environment.NewLine + f(1), f(2)}).ToList()
DataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True
DataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
For Each row As String() In InputList
DataGridView1.Rows.Add(row)
Next
This is the result:
Upvotes: 0
Reputation: 15091
I get an error with your code; however I am not familiar with Regex. A simple String.Split will accomplish the same thing. The .Split overload I used takes a Sting array. We only need an array of 1 element.The array is initialized right in the Dim statement. The second parameter is necessary. With this start maybe you can do the rest.
Private Sub ParseString()
Dim RawNote As String = "{[Entry Number: 1 | @AppUser: James | @AppTimeDate: 5/21/2018 | @AppText: A lot of multiline text here]}{[Entry Number: 2 | @AppUser: John | @AppTimeDate: 5/22/2018 | @AppText: More multiline text here]}"
Dim Splitter() As String = {"{[Entry Number: "}
Dim substrings() As String = RawNote.Split(Splitter, StringSplitOptions.RemoveEmptyEntries)
For Each match As String In substrings
MsgBox(match)
Next
End Sub
Upvotes: 1