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
Matti Tiira
NA
46
9.3k
Csv importing to datagridview with commas and double quotes
Mar 21 2019 4:39 AM
Hi!
I have made a windows forms application which I can open and modify csv files with.
I have an other program where the csv file is made with.
The problem is that the csv file has commas and double quotes as delimiters.
How can I trim the quotes so they dont appear in the datagridview?
Now it looks like this in my program.
And my code for the import looks like this:
private
void
btnImport_Click(
object
sender, EventArgs e)
{
openFileDialog1.ShowDialog();
txtFilePath.Text = openFileDialog1.FileName;
BindDataCSV(txtFilePath.Text);
}
private
void
BindDataCSV(
string
filePath)
{
DataTable dt =
new
DataTable();
string
[] lines = System.IO.File.ReadAllLines(filePath);
if
(lines.Length > 0)
{
string
firstLine = lines[0];
string
[] headerLabels = firstLine.Split(
','
);
foreach
(
string
headerWord
in
headerLabels)
{
dt.Columns.Add(
new
DataColumn(headerWord));
}
for
(
int
r = 1; r < lines.Length; r++)
{
string
[] dataWords = lines[r].Split(
','
);
DataRow dr = dt.NewRow();
int
columnIndex = 0;
foreach
(
string
headerWord
in
headerLabels)
{
dr[headerWord] = dataWords[columnIndex++];
}
dt.Rows.Add(dr);
}
}
if
(dt.Rows.Count > 0)
{
dataGridView1.DataSource = dt;
}
}
I would really appreciate all help, Thanks
-Matti
Reply
Answers (
6
)
Copy only new or modified files/directories in C#
Reg: Google Sheets Refresh functionality