EDN Admin
Well-known member
Hey guys, Im trying to import a text file into a listview control. The control has 2 columns, and Id like to split the data loaded into each column. Than for each line into a new row. for example my text file will format.
Name:Andrew
Age:22
Gender:Male
Hobbies:Sports
Therefore it should add the first keyword Name to the first column than split at the colon than add Andrew to the 2nd column. Than start a new row for Age:22, Can you guys please help me out here.
Thus what I have so far. // Ask the user to select a file
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Text files (*.txt)|*.txt";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
// Clear listview
listView.Items.Clear();
// Load text
using (TextReader textReader = new StreamReader(openFileDialog.FileName))
{
// Load the text line by line
string line = string.Empty;
string[] cut = line.Split);
while ((line = textReader.ReadLine()) != null)
{
// Append
ListViewItem item = new ListViewItem(new string[] { cut[0], cut[2] });
listView.Items.Add(item);
}
}
}
}
View the full article
Name:Andrew
Age:22
Gender:Male
Hobbies:Sports
Therefore it should add the first keyword Name to the first column than split at the colon than add Andrew to the 2nd column. Than start a new row for Age:22, Can you guys please help me out here.
Thus what I have so far. // Ask the user to select a file
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Text files (*.txt)|*.txt";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
// Clear listview
listView.Items.Clear();
// Load text
using (TextReader textReader = new StreamReader(openFileDialog.FileName))
{
// Load the text line by line
string line = string.Empty;
string[] cut = line.Split);
while ((line = textReader.ReadLine()) != null)
{
// Append
ListViewItem item = new ListViewItem(new string[] { cut[0], cut[2] });
listView.Items.Add(item);
}
}
}
}
View the full article