Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. DataGridView to XLS/CSV

DataGridView to XLS/CSV

Scheduled Pinned Locked Moved C#
helpworkspace
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vandretta
    wrote on last edited by
    #1

    G'day all, I'm writing an application that requires me to output the values stored within a DataGridView to an cls/csv file (or basically anything that M$ Excel can read). This is as far as i have gotten:

            string strValue = " ";
            for (int i = 0; i < stocksDataGridView.Rows.Count - 1; i++)
            {
                for (int j = 0; j < stocksDataGridView.Rows\[i\].Cells.Count; j++)
                {
                    if (!string.IsNullOrEmpty(stocksDataGridView\[j, i\].Value.ToString()))
                    {
                        if (j > 0)
                            strValue += "," + stocksDataGridView\[j, i\].Value.ToString();
                        else
                        {
                            if (!String.IsNullOrEmpty(strValue))
                                strValue = stocksDataGridView\[j, i\].Value.ToString();
                            else
                                strValue = strValue + Environment.NewLine + stocksDataGridView\[j, i\].Value.ToString();
                        }
                    }
                }
                strValue = strValue + Environment.NewLine;
            }
    
            string fileName = @saveFileDialog1.FileName;
            if (File.Exists(fileName) && !String.IsNullOrEmpty(strValue))
            {
                File.WriteAllText(fileName, strValue);
            }
    

    I'm sure its not very pretty and i have no idea how/where/why i have it in there, but all i can say is that it doesn't actually output anything. Any and all help will be greatly appreciated. Thanks in Advance, Tom

    S 1 Reply Last reply
    0
    • V Vandretta

      G'day all, I'm writing an application that requires me to output the values stored within a DataGridView to an cls/csv file (or basically anything that M$ Excel can read). This is as far as i have gotten:

              string strValue = " ";
              for (int i = 0; i < stocksDataGridView.Rows.Count - 1; i++)
              {
                  for (int j = 0; j < stocksDataGridView.Rows\[i\].Cells.Count; j++)
                  {
                      if (!string.IsNullOrEmpty(stocksDataGridView\[j, i\].Value.ToString()))
                      {
                          if (j > 0)
                              strValue += "," + stocksDataGridView\[j, i\].Value.ToString();
                          else
                          {
                              if (!String.IsNullOrEmpty(strValue))
                                  strValue = stocksDataGridView\[j, i\].Value.ToString();
                              else
                                  strValue = strValue + Environment.NewLine + stocksDataGridView\[j, i\].Value.ToString();
                          }
                      }
                  }
                  strValue = strValue + Environment.NewLine;
              }
      
              string fileName = @saveFileDialog1.FileName;
              if (File.Exists(fileName) && !String.IsNullOrEmpty(strValue))
              {
                  File.WriteAllText(fileName, strValue);
              }
      

      I'm sure its not very pretty and i have no idea how/where/why i have it in there, but all i can say is that it doesn't actually output anything. Any and all help will be greatly appreciated. Thanks in Advance, Tom

      S Offline
      S Offline
      SeMartens
      wrote on last edited by
      #2

      Hi Tom, are you sure that the command "File.WriteAllText" is executed? You only enter the if-block if your file exists! Maybe you have forgotten a "!" before "File.Exists"? Regards Sebastian

      It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

      V 1 Reply Last reply
      0
      • S SeMartens

        Hi Tom, are you sure that the command "File.WriteAllText" is executed? You only enter the if-block if your file exists! Maybe you have forgotten a "!" before "File.Exists"? Regards Sebastian

        It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

        V Offline
        V Offline
        Vandretta
        wrote on last edited by
        #3

        Thankyou so very much. I can't believe I would screw up something like that! Man I must be getting old... Thankyou again, Tom

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups