0

I am working with a small application, where user can write datagrid content to text file. I use this code below to get data from SQL to Data Grid

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

     }

And this code I used to write the data Grid data to a text file

    public static void WriteDataToFile(DataTable submittedDataTable, string submittedFilePath)
    {
        int i = 0;
        StreamWriter sw = null;

        sw = new StreamWriter(submittedFilePath, false);

        for (i = 0; i < submittedDataTable.Columns.Count - 1; i++)
        {

            sw.Write(submittedDataTable.Columns[i].ColumnName + ";");

        }
        sw.Write(submittedDataTable.Columns[i].ColumnName);
        sw.WriteLine();

        foreach (DataRow row in submittedDataTable.Rows)
        {
            object[] array = row.ItemArray;

            for (i = 0; i < array.Length - 1; i++)
            {
                sw.Write(array[i].ToString() + ";");
            }
            sw.Write(array[i].ToString());
            sw.WriteLine();

        }

        sw.Close();
    }

But when I used this code all the column are written to text file. My question is that where can I update the code to write only the specific columns( not all column)

portal
  • 31
  • 4
  • 1
    Possible duplicate of [select certain columns of a data table](https://stackoverflow.com/questions/13760072/select-certain-columns-of-a-data-table) – Evertude Aug 09 '18 at 10:31
  • What columns do you want to write to the file then? How do you identify them? – mm8 Aug 09 '18 at 11:08

1 Answers1

0

Try changes below :

        public static void WriteDataToFile(DataTable submittedDataTable, string submittedFilePath)
        {
            string[] columnNames = { "Column A", "Column B", "Column C", "Column D" };
            int[] columnIndexes = submittedDataTable.Columns.Cast<DataColumn>().Where(x => columnNames.Contains(x.ColumnName)).Select(x => x.Ordinal).ToArray();

            int i = 0;
            StreamWriter sw = null;

            sw = new StreamWriter(submittedFilePath, false);

            sw.WriteLine(string.Join(";", columnNames));

            foreach (DataRow row in submittedDataTable.Rows)
            {
                object[] array = row.ItemArray;

                string[] values = columnIndexes.Select(x => array[x].ToString()).ToArray();
                string csv = string.Join(";", values);

                sw.WriteLine(csv);
            }

            sw.Close();
        }
jdweng
  • 33,250
  • 2
  • 15
  • 20