0

I'm trying to develop an application which will take the data from a datagrid and based on a drop down menu choice return a csv file with only the selected client . My code is shown below , This links to a previous question I posted howeever I am still getting no values back and I really need to sort this out so I'm wondering if anyone can either see were i'm going wrong or else provide alternatives

    //Master inventory export 
    private void ExportClass_Click(object sender, EventArgs e)
    {
        StringBuilder str = new StringBuilder();
        objSqlCommands2 = new SqlCommands("MasterInventory", "ClientName");
        string strString = str.ToString();
        string Filepath = txtSaveShareClass.Text.ToString();
        str.Append("ISIN ,FundName,Status,Share CCY,Benchmark,NAV  Freq,CLASSCODE,SIMULATION,HEDGED,FUNDCCY");

        StringManipulation sm = new StringManipulation();

        foreach (DataRow dr in this.CalcDataSet.MasterInventory)
        {
            foreach (object field in dr.ItemArray)
            {
                str.Append(field.ToString() + ",");
             }

            str.Replace(",", "\n", str.Length - 1, 1);
        }

        try
        {
            System.IO.File.WriteAllText(Filepath, str.ToString());

        }
        catch (Exception ex)
        {
            MessageBox.Show("Write Error :" + ex.Message);
        }

        List<string[]> lsClientList =  objStringManipulation.parseCSV(Filepath,cmbClientList .Text.ToCharArray());


        foreach (string[] laClient in lsClientList)
        {
            sm.parseCSV2(Filepath, cmbClientList.Text.ToCharArray());
            List<string[]> newFoo = lsClientList.Where(x =>  x.Contains(cmbClientList.Text)).ToList();
            List<string[]> Results = sm.parseCSV2(Filepath,    cmbClientList.Text.ToCharArray()).Where(x => x.Contains(cmbClientList.Text)).ToList();

            //Refreshs the Client table on display from the 
            System.IO.File.WriteAllText(Filepath, Results.ToString());             
        }

        this.TableAdapter.Fill(this.CalcDataSet.MasterInventory);

        dataGridView2.Update();

    }
4

1 回答 1

1

如果您的所有变量都正确填写并且您的Results列表包含您期望的数据,那么问题出在您的WriteAllText电话上。你有:

System.IO.File.WriteAllText(Filepath, Results.ToString());

这不会产生您似乎期望的输出。它可能只会给你类名。

Results是一个List<string[]>。如果要将其输出为 CSV,则必须枚举它:

using (var outfile = new StreamWriter(Filepath))
{
    foreach (var line in Results)
    {
        StringBuilder sb = new StringBuilder();
        foreach (var field in line)
        {
            sb.Append(field + ",");
        }
        sb.Length = sb.Length -1;
        outfile.WriteLine(sb.ToString());
    }
}
于 2013-08-13T21:03:55.847 回答