title: Export DataGrid to text file
body: <p>I'm new to programming (1st year of learning at college) and I'm working on a small application.  </p>
  
  <p>I have a window where user can retrieve data from SQL to <code>DataGrid</code> and a <code>Button</code> for exporting some data from a <code>DataGrid</code> data to a text file. </p>
  
  <p>This is the code I've used to get data from SQL:</p>
  
  <pre><code>SqlConnection con = new SqlConnection("Server = localhost;Database = autoser; Integrated Security = true");
  SqlCommand cmd = new SqlCommand("selectproduct", con); // Using a Store Procedure.
  cmd.CommandType = CommandType.StoredProcedure;
  DataTable dt = new DataTable("dtList");
  cmd.Parameters.AddWithValue("@Code", txtbarcode.Text);
  
  SqlDataAdapter da = new SqlDataAdapter(cmd);
  da.Fill(dt);
  data.ItemsSource = dt.DefaultView;
  SqlDataAdapter adapt = new SqlDataAdapter(cmd);
  DataSet ds = new DataSet();
  adapt.Fill(ds);
  con.Close();
  int count = ds.Tables[0].Rows.Count;
  if (count == 0)
  {
      MessageBox.Show("This product doesn't excist");
      SystemSounds.Hand.Play();
  }
  else if (count == 1)
  {
      lblinfo.Visibility = Visibility.Visible;
      SystemSounds.Asterisk.Play();
  
  }
  </code></pre>
  
  <p>And this one is the code I used to write text file:</p>
  
  <pre><code>{
      using (StreamWriter writer = new StreamWriter("D:\\test.txt", true))
      {
          writer.WriteLine("Welcome");
          writer.WriteLine("E N T E R N E T");
      }
  
      using (StreamWriter writer = new StreamWriter("D:\\test.txt", true))
      {
          writer.WriteLine(data.Items);
      }
  
  
      using (StreamWriter writer = new StreamWriter("D:\\test.txt", true))
      {
          writer.WriteLine(data.Items);
      }
      // Append line to the file.
      using (StreamWriter writer = new StreamWriter("D:\\test.txt", true))
      {
          writer.WriteLine("---------------------------------------");
          writer.WriteLine("             Thank You!   ");
          writer.WriteLine("        " + DateTime.Now + "              ");
      }
  }
  </code></pre>
  
  <p>When I Open the text file i get this data</p>
  
  <pre><code>Welcome
  E N T E R N E T
  System.Windows.Controls.ItemCollection -   Why isn't show the data grid 
  data
  
  ---------------------------------------
      Thank You   
      7/26/2018 12:38:37 PM   
  </code></pre>
  
  <p>My question is: Where is my mistake that cause the data from the <code>DataGrid</code> to don't be showed in correct way?<br>
  Thanks in advance</p>