How to write a text file by clicking on a DataGridView?

0

I have a DataGridView and when I click this DataGridView I have to write to a file a select comparing a column of a table to another column of another table and what results from that select is what I have to write to the file.

This is how I am writing to the file at the moment but it is not done well because you are writing what it shows in DataGridView .

if (dataGridViewEnviarDados.SelectedRows.Count > 0)
{
     foreach (DataGridViewRow r in dataGridViewEnviarDados.SelectedRows)
     {
         var transactionHandler = new TransactionHandler();
         FileInfo arquivo = new FileInfo("C:\Users\HP8200\Desktop\faturas\" 
                   + r.Cells[0].Value.ToString() 
                   + r.Cells[1].Value.ToString() 
                   + r.Cells[2].Value.ToString() + ".txt");
         using (TextWriter tw = new StreamWriter(arquivo.FullName,false,Encoding.Default))
         {
            foreach (DataGridViewColumn c in dataGridViewEnviarDados.Columns)
            {
                  tw.Write(r.Cells[c.Name].Value.ToString() +";");
            }
         }
    }
}

This is how datagrindview

And the select I'm using is this:

SELECT  X.[TransDocument], X.[TransSerial],X.[TransDocNumber], I.[CreateDate], I.[PartyFederalTaxID], I.[TotalAmount], 
I.[ShipToAddressDetailLine1], I.[ShipToPostalCode], U.[ItemID], U.[TotalGrossAmount], U.[TotalNetPrintAmout], U.[Quantity], Y.[ItemID], Y.[BarCode], Y.[TaxableGroupID]

FROM
   [ELISAData].[dbo].[SaleTransaction] I
   INNER JOIN [ELISAData].[dbo].[SaleTransactionDetails] U
      ON I.[TransDocNumber] = U.[TransDocNumber]
      INNER JOIN [ELISAData].[dbo].[UXMenu] X
      ON U.[TransDocNumber] = X.[TransDocNumber]
      INNER JOIN [ELISAData].[dbo].[Item] Y
      ON U.[ItemID] = Y.[ItemID]

WHERE
   I.[TransDocNumber] = U.[TransDocNumber] AND
   X.[TransDocNumber] = U.[TransDocNumber] AND
   U.[ItemID] = Y.[ItemID]

   Order by [TransDocNumber]
    
asked by anonymous 29.08.2017 / 15:56

0 answers