0

我有以下代码从 a 获取数据DataGridView并将其写入 csv 文件。csv 文件有 5 列。但是,我只希望将选定客户端的数据写入文件,客户端名称是从组合框中选择的。下面的代码将所有数据网格值写入文件但是我只想写入 ClientName 等于组合框值的行但是我无法让它工作

  // inventory export 
        private void btnExportShareClass_Click( object sender, EventArgs e)
        {
            //SET GRID
            DataGridView gridIn ;
            string outputFile = Inv_Export_savePath.Text;

        gridIn = Inv_DataGrid;
        //VAR holding client combobox
        string Selected_Combo = Inv_ClientList_Export_Combobox.Text ;


        //test to see if the DataGridView has any rows
        if (gridIn.RowCount > 0)
        {
           string value = "";
           DataGridViewRow dr = new DataGridViewRow();
           StreamWriter swOut = new StreamWriter(outputFile);

           //write header rows to csv
           for (int i = 0; i <= gridIn.Columns.Count - 1; i++)
           {
              if (i > 0)
              {
                 swOut.Write(",");
              }
              swOut.Write(gridIn.Columns[i].HeaderText);
           }

           swOut.WriteLine();

           //write DataGridView rows to csv
           for (int j = 0; j <= gridIn.Rows.Count - 1; j++)
           {
              if (j > 0)
              {

                        swOut.WriteLine();

              }

              dr = gridIn.Rows[j];

              for (int i = 0; i <= gridIn.Columns.Count - 1; i++)
              {
                 if (i > 0)
                 {
                    swOut.Write(",");
                 }

                 value = dr.Cells[i].Value.ToString();
                 //replace comma's with spaces
                 value = value.Replace(',', ' ');
                 //replace embedded newlines with spaces
                 value = value.Replace(Environment.NewLine, " "); 

                 swOut.Write(value);
              }
           }
           swOut.Close();
        }

    }


         for (int i = 0; i <= gridIn.Columns.Count - 1; i++)
              {
                 if (i > 0)
                 {
                      gridIn.Rows[i].ToString().Contains(Inv_ClientList_Export_Combobox.Text );
                    swOut.Write(",");
                 }

                 value = dr.Cells[i].Value.ToString();
                 //replace comma's with spaces
                 value = value.Replace(',', ' ');
                 //replace embedded newlines with spaces
                 value = value.Replace(Environment.NewLine, " "); 

                 swOut.Write(value);
              }
4

1 回答 1

1

From your comments, it seems like you should rely on a simple condition to perform the filtering you want. Here you have a corrected version of your code:

//test to see if the DataGridView has any rows
if (gridIn.RowCount > 0)
{
    string value = "";
    DataGridViewRow dr = new DataGridViewRow();
    StreamWriter swOut = new StreamWriter(outputFile);

    //write header rows to csv
    for (int i = 0; i <= gridIn.Columns.Count - 1; i++)
    {
        if (i > 0)
        {
            swOut.Write(",");
        }
        swOut.Write(gridIn.Columns[i].HeaderText);
    }

    swOut.WriteLine();

    //write DataGridView rows to csv
    bool previousSkipped = false;
    for (int j = 0; j <= gridIn.Rows.Count - 1; j++)
    {
        if (j > 0 && !previousSkipped)
        {
            swOut.WriteLine();
        }

        dr = gridIn.Rows[j];

        for (int i = 0; i <= gridIn.Columns.Count - 1; i++)
        {
            if (dr.Cells[2].Value.ToString().ToLower().Contains(Selected_Combo.ToLower()))
            {
                if (i > 0)
                {
                    swOut.Write(",");
                }

                value = dr.Cells[i].Value.ToString();
                //replace comma's with spaces
                value = value.Replace(',', ' ');
                //replace embedded newlines with spaces
                value = value.Replace(Environment.NewLine, " ");

                swOut.Write(value);
                previousSkipped = false;
            }
            else
            {
                previousSkipped = true; //To avoid using swOut.WriteLine(); more than required
            }
        }
    }
    swOut.Close();
}

This code checks whether the value of the third column (index 2) in the given row equals the content of the variable Selected_Combo, by ignoring caps (comparison of both strings ToLower()); and only in case of meeting this condition writes the given cell to the file.

于 2013-08-18T11:54:27.000 回答