How to programatically insert lines in csv file based on quantity of specific recordHow to read a CSV file into a .NET DatatableHow can I read and parse CSV files in C++?How to create CSV Excel file C#?How to import CSV file data into a PostgreSQL table?CSV file written with Python has blank lines between each rowHow to import CSV file to MySQL tableSpecific elements from XML string to a datagridview or datatable (C#)Read specific columns from a csv file with csv module?MS ACCESS insert records base on quantity numberCreate .CSV File inserting values
Was it really necessary for the Lunar Module to have 2 stages?
Why didn't this hurt this character as badly?
Why do computer-science majors learn calculus?
Illegal assignment from SObject to Contact
Nginx subdirectory wordpress wp-login redirects to 404 not found
How to replace the "space symbol" (squat-u) in listings?
Upright [...] in italics quotation
How to creep the reader out with what seems like a normal person?
How to verbalise code in Mathematica?
Why are the 2nd/3rd singular forms of present of « potere » irregular?
What is the strongest case that can be made in favour of the UK regaining some control over fishing policy after Brexit?
How to figure out whether the data is sample data or population data apart from the client's information?
Can someone publish a story that happened to you?
Modify locally tikzset
Minimum value of 4 digit number divided by sum of its digits
What does "rf" mean in "rfkill"?
Normal subgroup of even order whose nontrivial elements form a single conjugacy class is abelian
Do I have an "anti-research" personality?
Pressure to defend the relevance of one's area of mathematics
What is the difference between `a[bc]d` (brackets) and `ab,cd` (braces)?
Will tsunami waves travel forever if there was no land?
Were there two appearances of Stan Lee?
Python "triplet" dictionary?
What is a Recurrent Neural Network?
How to programatically insert lines in csv file based on quantity of specific record
How to read a CSV file into a .NET DatatableHow can I read and parse CSV files in C++?How to create CSV Excel file C#?How to import CSV file data into a PostgreSQL table?CSV file written with Python has blank lines between each rowHow to import CSV file to MySQL tableSpecific elements from XML string to a datagridview or datatable (C#)Read specific columns from a csv file with csv module?MS ACCESS insert records base on quantity numberCreate .CSV File inserting values
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have a program that runs a SQL query and returns data to a datagridview.

I then write the data to a csv file which works great.

However, I need to add a line in the csv for each item based on the quantity of the record.
The first record (highlighted in blue) has a quantity of 12, so I need to insert 12 lines containing the information from the first record, and so forth down the list.
This is what I would like to show :
Below is the code that I am using to create the csv file.
string strValue = string.Empty;
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
for (int j = 0; j < dataGridView1.Rows[i].Cells.Count; j++)
if (!string.IsNullOrEmpty(dataGridView1[j, i].Value.ToString()))
if (j > 0)
strValue = strValue + "," + dataGridView1[j, i].Value.ToString();
else
if (string.IsNullOrEmpty(strValue))
strValue = dataGridView1[j, i].Value.ToString();
else
strValue = strValue + Environment.NewLine + dataGridView1[j, i].Value.ToString();
string strFile = @"\\Path_To_CSV\DynamicsPartsLabels.csv";
if (File.Exists(strFile) && !string.IsNullOrEmpty(strValue))
File.WriteAllText(strFile, strValue);
Is there a way to read the last column in the datagridview (Quantity) and create a line in a csv for each record based on the number in that column?
c# winforms csv datagridview export-to-csv
add a comment |
I have a program that runs a SQL query and returns data to a datagridview.

I then write the data to a csv file which works great.

However, I need to add a line in the csv for each item based on the quantity of the record.
The first record (highlighted in blue) has a quantity of 12, so I need to insert 12 lines containing the information from the first record, and so forth down the list.
This is what I would like to show :
Below is the code that I am using to create the csv file.
string strValue = string.Empty;
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
for (int j = 0; j < dataGridView1.Rows[i].Cells.Count; j++)
if (!string.IsNullOrEmpty(dataGridView1[j, i].Value.ToString()))
if (j > 0)
strValue = strValue + "," + dataGridView1[j, i].Value.ToString();
else
if (string.IsNullOrEmpty(strValue))
strValue = dataGridView1[j, i].Value.ToString();
else
strValue = strValue + Environment.NewLine + dataGridView1[j, i].Value.ToString();
string strFile = @"\\Path_To_CSV\DynamicsPartsLabels.csv";
if (File.Exists(strFile) && !string.IsNullOrEmpty(strValue))
File.WriteAllText(strFile, strValue);
Is there a way to read the last column in the datagridview (Quantity) and create a line in a csv for each record based on the number in that column?
c# winforms csv datagridview export-to-csv
add a comment |
I have a program that runs a SQL query and returns data to a datagridview.

I then write the data to a csv file which works great.

However, I need to add a line in the csv for each item based on the quantity of the record.
The first record (highlighted in blue) has a quantity of 12, so I need to insert 12 lines containing the information from the first record, and so forth down the list.
This is what I would like to show :
Below is the code that I am using to create the csv file.
string strValue = string.Empty;
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
for (int j = 0; j < dataGridView1.Rows[i].Cells.Count; j++)
if (!string.IsNullOrEmpty(dataGridView1[j, i].Value.ToString()))
if (j > 0)
strValue = strValue + "," + dataGridView1[j, i].Value.ToString();
else
if (string.IsNullOrEmpty(strValue))
strValue = dataGridView1[j, i].Value.ToString();
else
strValue = strValue + Environment.NewLine + dataGridView1[j, i].Value.ToString();
string strFile = @"\\Path_To_CSV\DynamicsPartsLabels.csv";
if (File.Exists(strFile) && !string.IsNullOrEmpty(strValue))
File.WriteAllText(strFile, strValue);
Is there a way to read the last column in the datagridview (Quantity) and create a line in a csv for each record based on the number in that column?
c# winforms csv datagridview export-to-csv
I have a program that runs a SQL query and returns data to a datagridview.

I then write the data to a csv file which works great.

However, I need to add a line in the csv for each item based on the quantity of the record.
The first record (highlighted in blue) has a quantity of 12, so I need to insert 12 lines containing the information from the first record, and so forth down the list.
This is what I would like to show :
Below is the code that I am using to create the csv file.
string strValue = string.Empty;
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
for (int j = 0; j < dataGridView1.Rows[i].Cells.Count; j++)
if (!string.IsNullOrEmpty(dataGridView1[j, i].Value.ToString()))
if (j > 0)
strValue = strValue + "," + dataGridView1[j, i].Value.ToString();
else
if (string.IsNullOrEmpty(strValue))
strValue = dataGridView1[j, i].Value.ToString();
else
strValue = strValue + Environment.NewLine + dataGridView1[j, i].Value.ToString();
string strFile = @"\\Path_To_CSV\DynamicsPartsLabels.csv";
if (File.Exists(strFile) && !string.IsNullOrEmpty(strValue))
File.WriteAllText(strFile, strValue);
Is there a way to read the last column in the datagridview (Quantity) and create a line in a csv for each record based on the number in that column?
c# winforms csv datagridview export-to-csv
c# winforms csv datagridview export-to-csv
edited Mar 22 at 18:52
Risto M
2,145620
2,145620
asked Mar 22 at 17:55
DuncanDuncan
236
236
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Try changing the code to:
StringBuilder rows = new StringBuilder();
var row = string.Empty;
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
row = string.Join(",", dataGridView1.Rows[i].Cells.Cast<DataGridViewCell>().Select(x => x.Value));
int.TryParse(dataGridView1.Rows[i].Cells["Quantity"].Value?.ToString(), out var quantity);
for (int q = 0; q < quantity; q++)
rows.AppendLine(row);
var strValue = rows.ToString();
Unfortunately that creates a CSV with one record written one time (the last record in the datagrid view) with the quantity field included
– Duncan
Mar 25 at 2:36
would you be able set a breakpoint inside the for loop to debug and figure out what is happening?
– Matt.G
Mar 25 at 12:18
Changeindex++toq++. That said, I can't see why this isn't producing more records, unlessquantityis not getting the value out as expected.
– OhBeWise
Mar 25 at 12:45
Thanks @OhBeWise, I've edited the post to fix it. not sure how it only writes one line to the csv.
– Matt.G
Mar 25 at 12:48
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55305322%2fhow-to-programatically-insert-lines-in-csv-file-based-on-quantity-of-specific-re%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try changing the code to:
StringBuilder rows = new StringBuilder();
var row = string.Empty;
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
row = string.Join(",", dataGridView1.Rows[i].Cells.Cast<DataGridViewCell>().Select(x => x.Value));
int.TryParse(dataGridView1.Rows[i].Cells["Quantity"].Value?.ToString(), out var quantity);
for (int q = 0; q < quantity; q++)
rows.AppendLine(row);
var strValue = rows.ToString();
Unfortunately that creates a CSV with one record written one time (the last record in the datagrid view) with the quantity field included
– Duncan
Mar 25 at 2:36
would you be able set a breakpoint inside the for loop to debug and figure out what is happening?
– Matt.G
Mar 25 at 12:18
Changeindex++toq++. That said, I can't see why this isn't producing more records, unlessquantityis not getting the value out as expected.
– OhBeWise
Mar 25 at 12:45
Thanks @OhBeWise, I've edited the post to fix it. not sure how it only writes one line to the csv.
– Matt.G
Mar 25 at 12:48
add a comment |
Try changing the code to:
StringBuilder rows = new StringBuilder();
var row = string.Empty;
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
row = string.Join(",", dataGridView1.Rows[i].Cells.Cast<DataGridViewCell>().Select(x => x.Value));
int.TryParse(dataGridView1.Rows[i].Cells["Quantity"].Value?.ToString(), out var quantity);
for (int q = 0; q < quantity; q++)
rows.AppendLine(row);
var strValue = rows.ToString();
Unfortunately that creates a CSV with one record written one time (the last record in the datagrid view) with the quantity field included
– Duncan
Mar 25 at 2:36
would you be able set a breakpoint inside the for loop to debug and figure out what is happening?
– Matt.G
Mar 25 at 12:18
Changeindex++toq++. That said, I can't see why this isn't producing more records, unlessquantityis not getting the value out as expected.
– OhBeWise
Mar 25 at 12:45
Thanks @OhBeWise, I've edited the post to fix it. not sure how it only writes one line to the csv.
– Matt.G
Mar 25 at 12:48
add a comment |
Try changing the code to:
StringBuilder rows = new StringBuilder();
var row = string.Empty;
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
row = string.Join(",", dataGridView1.Rows[i].Cells.Cast<DataGridViewCell>().Select(x => x.Value));
int.TryParse(dataGridView1.Rows[i].Cells["Quantity"].Value?.ToString(), out var quantity);
for (int q = 0; q < quantity; q++)
rows.AppendLine(row);
var strValue = rows.ToString();
Try changing the code to:
StringBuilder rows = new StringBuilder();
var row = string.Empty;
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
row = string.Join(",", dataGridView1.Rows[i].Cells.Cast<DataGridViewCell>().Select(x => x.Value));
int.TryParse(dataGridView1.Rows[i].Cells["Quantity"].Value?.ToString(), out var quantity);
for (int q = 0; q < quantity; q++)
rows.AppendLine(row);
var strValue = rows.ToString();
edited Mar 25 at 12:48
answered Mar 22 at 18:30
Matt.GMatt.G
2,7591416
2,7591416
Unfortunately that creates a CSV with one record written one time (the last record in the datagrid view) with the quantity field included
– Duncan
Mar 25 at 2:36
would you be able set a breakpoint inside the for loop to debug and figure out what is happening?
– Matt.G
Mar 25 at 12:18
Changeindex++toq++. That said, I can't see why this isn't producing more records, unlessquantityis not getting the value out as expected.
– OhBeWise
Mar 25 at 12:45
Thanks @OhBeWise, I've edited the post to fix it. not sure how it only writes one line to the csv.
– Matt.G
Mar 25 at 12:48
add a comment |
Unfortunately that creates a CSV with one record written one time (the last record in the datagrid view) with the quantity field included
– Duncan
Mar 25 at 2:36
would you be able set a breakpoint inside the for loop to debug and figure out what is happening?
– Matt.G
Mar 25 at 12:18
Changeindex++toq++. That said, I can't see why this isn't producing more records, unlessquantityis not getting the value out as expected.
– OhBeWise
Mar 25 at 12:45
Thanks @OhBeWise, I've edited the post to fix it. not sure how it only writes one line to the csv.
– Matt.G
Mar 25 at 12:48
Unfortunately that creates a CSV with one record written one time (the last record in the datagrid view) with the quantity field included
– Duncan
Mar 25 at 2:36
Unfortunately that creates a CSV with one record written one time (the last record in the datagrid view) with the quantity field included
– Duncan
Mar 25 at 2:36
would you be able set a breakpoint inside the for loop to debug and figure out what is happening?
– Matt.G
Mar 25 at 12:18
would you be able set a breakpoint inside the for loop to debug and figure out what is happening?
– Matt.G
Mar 25 at 12:18
Change
index++ to q++. That said, I can't see why this isn't producing more records, unless quantity is not getting the value out as expected.– OhBeWise
Mar 25 at 12:45
Change
index++ to q++. That said, I can't see why this isn't producing more records, unless quantity is not getting the value out as expected.– OhBeWise
Mar 25 at 12:45
Thanks @OhBeWise, I've edited the post to fix it. not sure how it only writes one line to the csv.
– Matt.G
Mar 25 at 12:48
Thanks @OhBeWise, I've edited the post to fix it. not sure how it only writes one line to the csv.
– Matt.G
Mar 25 at 12:48
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55305322%2fhow-to-programatically-insert-lines-in-csv-file-based-on-quantity-of-specific-re%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown