TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
vinod chand
NA
16
10k
Importing space delimited TEXT file into a DataGridView C#
May 12 2015 8:55 AM
I want upload a text file having multiple values with multiple blank space delimiter. If there is a single space between two words, code is working fine and result is coming properly. but for many value there is more then one space delimiter available between two words. In this case, code is reading it wrongly and creating multiple columns for each space delimiter.
Written code is given below....
private void button2_Click(object sender, EventArgs e)
{
dataGridView1.DataSource = Helper.DataTableFromTextFile("*.txt",'"');
}
public class Helper
{
public static DataTable DataTableFromTextFile(string location, char delimiter = '"')
{
Stream mystream;
OpenFileDialog opentext = new OpenFileDialog();
if (opentext.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if ((mystream = opentext.OpenFile()) != null)
{
location = opentext.FileName;
}
}
DataTable result;
string[] LineArray = File.ReadAllLines(location);
result = FormDataTable(LineArray, delimiter);
return result;
}
private static DataTable FormDataTable(string[] LineArray, char delimiter)
{
DataTable dt = new DataTable();
AddColumnToTable(LineArray, delimiter, ref dt);
AddRowToTable(LineArray, delimiter, ref dt);
return dt;
}
private static void AddRowToTable(string[] valueCollection, char delimiter, ref DataTable dt)
{
for (int i = 1; i < valueCollection.Length; i++)
{
string[] values = valueCollection[i].Split(' ');
DataRow dr = dt.NewRow();
for (int j = 0; j < values.Length; j++)
{
dr[j] = values[j];
}
dt.Rows.Add(dr);
}
}
private static void AddColumnToTable(string[] columnCollection, char delimiter, ref DataTable dt)
{
string[] columns = columnCollection[0].Split(' ');
foreach (string columnName in columns)
{
DataColumn dc = new DataColumn(columnName, typeof(string));
dt.Columns.Add(dc);
}
}
public static string strfilename { get; set; }
}
}
End......
Text file data to port...
Emp_ID Date Time T Code
50 5/7/2015 07:23:01 PM 8
50 5/9/2015 07:02:43 AM 8
50 5/9/2015 07:26:53 PM 8
50 5/10/2015 07:01:15 AM 8
69 5/7/2015 05:48:59 PM 5
69 5/9/2015 01:51:37 PM 5
111 5/7/2015 05:42:22 PM 8
111 5/7/2015 05:42:28 PM 8
208 5/9/2015 02:01:26 PM 5
208 5/10/2015 08:18:33 AM 5
229 5/7/2015 05:42:57 PM 8
229 5/7/2015 05:43:02 PM 8
229 5/9/2015 05:39:54 PM 8
1028 5/9/2015 07:26:27 PM 8
1028 5/10/2015 07:06:31 AM 8
1065 5/7/2015 06:48:07 PM 8
1065 5/9/2015 07:22:09 AM 8
1065 5/9/2015 04:11:39 PM 8
1065 5/10/2015 08:18:40 AM 5
1072 5/9/2015 08:00:01 AM 5
1072 5/9/2015 01:55:28 PM 5
113522 5/7/2015 05:42:41 PM 8
113522 5/7/2015 05:42:44 PM 8
117322 5/7/2015 08:25:59 PM 5
117322 5/9/2015 08:11:49 AM 5
117322 5/9/2015 01:57:02 PM 5
117322 5/10/2015 08:30:51 AM 5
146582 5/7/2015 05:58:37 PM 8
146582 5/9/2015 06:02:40 PM 8
152588 5/9/2015 06:27:05 AM 8
152588 5/9/2015 06:11:23 PM 8
152588 5/9/2015 06:11:32 PM 8
152588 5/10/2015 07:09:05 AM 8
156586 5/9/2015 09:45:48 AM 8
156586 5/9/2015 06:15:30 PM 8
Output is given below...
Please help in this issue where multiple space delimiter is not accommodate in the data grid while uploading the text sheet.
Thanks in advance...
Reply
Answers (
2
)
Winforms Comobobox with filter
how to print more than one page using e.graphics c#