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;








0















I have a program that runs a SQL query and returns data to a datagridview.



DataGridView Example



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



enter image description here



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 :
enter image description here



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?










share|improve this question






























    0















    I have a program that runs a SQL query and returns data to a datagridview.



    DataGridView Example



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



    enter image description here



    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 :
    enter image description here



    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?










    share|improve this question


























      0












      0








      0








      I have a program that runs a SQL query and returns data to a datagridview.



      DataGridView Example



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



      enter image description here



      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 :
      enter image description here



      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?










      share|improve this question
















      I have a program that runs a SQL query and returns data to a datagridview.



      DataGridView Example



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



      enter image description here



      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 :
      enter image description here



      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 22 at 18:52









      Risto M

      2,145620




      2,145620










      asked Mar 22 at 17:55









      DuncanDuncan

      236




      236






















          1 Answer
          1






          active

          oldest

          votes


















          0














          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();





          share|improve this answer

























          • 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











          • 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











          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
          );



          );













          draft saved

          draft discarded


















          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









          0














          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();





          share|improve this answer

























          • 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











          • 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















          0














          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();





          share|improve this answer

























          • 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











          • 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













          0












          0








          0







          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();





          share|improve this answer















          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();






          share|improve this answer














          share|improve this answer



          share|improve this answer








          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











          • 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

















          • 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











          • 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
















          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



















          draft saved

          draft discarded
















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

          용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

          155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해