使用app.config和C#app中的数据进行数据表过滤 [英] Datatable filter using data in app.config and C# app
问题描述
场景:我有一个数据表,可以动态地将数据填充到数据网格/运行时间,如下所示:
Scenario: I have a datatable which fills data into datagrid dynamically/run time as shown below:
dt.Rows.Add(value1, value2, value3, value4, value5, value6, value6, value7, value8);
现在我在app中有数据我检索的.config文件如下所示:
Now I have data in app.config file which I retrieve as shown below:
// app.config file contains string with comma separated and I am splitting it in code
string num1 = ConfigurationManager.AppSettings["crNum"];
string[] values = num1 .Split(',');
foreach (string crNum in values)
{
// I need code here to delete the entire rows where value1 == crNum
// in my above data table dt and also when I reopen/refresh
// my c# application I should not see the row deleted before.
}
最后我将数据表插入网格
Finally I insert the datatable into the grid
myCRGrid.DataSource = dt;
我是C#的新手,我道歉对于这样一个奇怪的问题。
请帮助
谢谢!
我尝试了什么:
i尝试删除但我没有成功,也得到了co nfused在foreach循环中编写代码
I am new to C#, I apologise for such a weird question.
Please help
Thanks!
What I have tried:
i tried deleting but im not successful, also getting confused to write the code inside the foreach loop
推荐答案
asummingdemodata.csv看起来像这样
asumming that "demodata.csv" looks like this
42,1,2,3,4,5,6,7
43,7,6,5,4,3,2,1
42,2,3,4,5,6,7,8
44,9,8,7,6,5,4,3
i会建议类似的东西
i would suggest something like that
void Main()
{
var records = ProcessMyData(@"c:\temp\demodata.csv");
var filteredRecords =
records.Where(x => x.Value1 != "42");
// now you have the records of the file you want
// you may assign that without use of a datatable to your grid
}
// Define other methods and classes here
class MyData
{
public string Value1 { get; set; }
public string Value2 { get; set; }
public string Value3 { get; set; }
internal static MyData ParseFromCsv(string line)
{
var c = line.Split(',');
return new MyData
{
Value1 = c[0],
Value2 = c[1],
Value3 = c[2]
//....
};
}
}
private static List<MyData> ProcessMyData(string path)
{
return File.ReadAllLines(path)
.Select(MyData.ParseFromCsv)
.ToList();
}
试试这个
try this
foreach (DataRow row in dt.Rows)
{
if (values.Contains(row["Value1"].ToString()))
row.Delete();
}
dt.AcceptChanges();
现在试试下面的代码吧。它通过select方法从founden删除你的值包含行。选择方法是返回ICollection< datarow>
Ok now try below code. Its remove your value contain rows by founden from select method. Select method is return ICollection<datarow>
string num1 = ConfigurationManager.AppSettings["crNum"];
string[] values = num1 .Split(',');
foreach (string crNum in values)
{
foreach (var nrow in dt.Select("Value1 = " + crNum))
{
dt.Rows.Remove(nrow);
}
}
这篇关于使用app.config和C#app中的数据进行数据表过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!