问题
我有一个包含此样式数据的文本文件:day,time,logtype,sessionID,loginID
例如:
2017-12-12 21:59:13,739 [7] INFO - Login.LoginUser_Authenticate - o03dx1c3hnfxz35ljee1mfcy未知 - 登录:BST028991
2017-12-12 23:59:13,839 [17] INFO - Login.LoginUser_Authenticate - o03dx1c3hnfxz35ljee1mfcy未知 - 登录:BST345991
等.....
我想将文本文件导出到ListView enter image description here
我创建了一个包含5列的datagridview(文本文件中的每个数据字段都有一列)。
我用这段代码尝试了它
Private Sub SetListView(ByRef myListView As ListView)
myListView.View = View.Details
With myListView.Columns
.Add("day", 90)
.Add("time", 100)
.Add("logtype", 90)
.Add("sessionid", 170)
.Add("loginid", 150)
End With
End Sub
Private Sub OpenFolderToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenFolderToolStripMenuItem.Click
Dim TextFile As IO.StreamReader
Dim Line As String
OpenFileDialog1.Filter = "LOG FILE|*.log|TEXT FILE|*.text|ALL FILE|*.*"
If OpenFileDialog1.ShowDialog = DialogResult.OK Then
For x = 0 To OpenFileDialog1.FileNames.Count - 1
MessageBox.Show(OpenFileDialog1.FileNames(x))
Me.LogListView.Items.Clear()
TextFile = New IO.StreamReader(OpenFileDialog1.FileNames(x), System.Text.Encoding.Default)
Line = TextFile.ReadLine()
Do While Line <> Nothing
Me.LogListView.Items.Add(Line)
Line = TextFile.ReadLine()
Loop
TextFile.Close()
Next
End If
End Sub
他的代码错了,当它加载时,文本只有一列 enter image description here
答案 0 :(得分:0)
你需要爆炸该行并为多列listviewitem add创建新的ListViewItem 请参考以下代码:
String[] arr = new String[5];
arr = explode(line);
var item = new ListViewItem(new[] {arr[0],arr[1],arr[2],arr[3],arr[4]});
myListView.Items.Add(item);
这是&#34; Lightning3&#34;代码,我帮助做格式化。
Dim arr As [String]() = New [String](4) {}
arr = explode(line)
Dim item = New ListViewItem(New () {arr(0), arr(1), arr(2), arr(3), arr(4)})
myListView.Items.Add(item)