筛选要添加到字符串生成器中的数据网格元素
本文关键字:数据 数据网 元素 网格 添加 字符串 筛选 | 更新日期: 2023-09-27 18:27:24
我的应用程序有一个导出按钮,可以打开一个保存文件对话框和一个下拉框,允许用户选择客户端和保存文件的路径。单击导出按钮后,我想从客户端名称与下拉框中找到的名称匹配的数据网格中获取数据,并将这些数据发送到文件中,目前我的代码如下,但它只返回列的标题,有人知道解决方案吗?:
foreach (DataRow dr in this.CalcDataSet.MInve)
{
bool hasValue = false;
for (int i = 0; i < dr.ItemArray.Count(); i++)
{
//if doesnt match selected client
if (!dr[i].ToString().Contains(dropboxClientList.SelectedValue.ToString()))
hasValue = true;
}
//else
if (!hasValue) rowsToADD.Add(dr);
foreach (DataRow field in rowsToADD)
{
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);
}
取自此SO后
string file_name = "C:''test1.txt";
System.IO.StreamWriter objWriter;
objWriter = new System.IO.StreamWriter(file_name);
int count = dgv.Rows.Count;
for (int row = 0; row < count; row++)
{
int colCount = dgv.Rows[row].Cells.Count;
// EDIT inserted if statement
string selectedValue = dropboxClientList.SelectedValue.ToString();
string clientName = dgv.Rows[row].Cells[1].Value.ToString();
// If the current row contains the selected client name
if (clientName.Contains(selectedValue)
{
// Write the columns for the current row
for ( int col = 0; col < colCount; col++)
{
objWriter.WriteLine(dgv.Rows[row].Cells[col].Value.ToString());
}
}
// record seperator could be written here.
}
objWriter.Close();