过滤行写

时间:2013-08-18 10:56:02

标签: c# file-io datagridview streamwriter

我有以下代码从DataGridView获取数据并将其写入csv文件。 csv文件有5列。但是,我只希望将所选客户端的数据写入文件,从comboBox中选择客户端名称。下面的代码将所有datagrid值写入文件,但是我只想写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);
              }

1 个答案:

答案 0 :(得分:1)

从您的评论中,您似乎应该依靠简单的条件来执行您想要的过滤。您有代码的更正版本:

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

此代码通过忽略大写字母(两个字符串Selected_Combo的比较)来检查给定行中第三列(索引2)的值是否等于变量ToLower()的内容。并且仅在满足此条件的情况下将给定单元格写入文件。