我有以下代码从 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);
}