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

Asked

Viewed 52 times

0

I have a DataGridView and by clicking on that DataGridView I have to write a file select comparing one column of a table to another column of another table and what results from that select is what I have to write in the file.

This is how I am writing in the file right now but it is not well done as it is 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 the datagrindview inserir a descrição da imagem aqui

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]
  • It’s hard to understand what you need to do!

  • I’ll try to explain myself better, I need to make a file . txt , the contents of the file must be the select of the comparison of two tables

  • @Rovannlinhalis did not understand what you need sorry

  • exactly how the datagridview has to be displayed, structure of your tables and the selects you have to run

  • @Rovannlinhalis already updated

  • the select, before there were 2, already joined with the Inner Join as it had commented ?

  • @Rovannlinhalis because I wanted to facilitate the way to explain makes with only two tables I try to adapt, only I put this select because you asked to by select what I use

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.