Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Scenario: I have a datatable which fills data into datagrid dynamically/run time as shown below:

C#
dt.Rows.Add(value1, value2, value3, value4, value5, value6, value6, value7, value8);


Now I have data in app.config file which I retrieve as shown below:

C#
// 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

C#
myCRGrid.DataSource = dt;


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
Posted
Updated 27-Aug-16 10:59am
Comments
FranzBe 26-Aug-16 16:06pm    
app.config is not expected to hold application data, you should use some .csv file for that; the app.config should only hold the 'configuration' related stuff. It would help understand your question if you post some sample data

asumming that "demodata.csv" looks like this

C#
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 would suggest something like that

C#
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();
}
 
Share this answer
 
try this

C#
foreach (DataRow row in dt.Rows)
       {
           if (values.Contains(row["Value1"].ToString()))
               row.Delete();
       }
       dt.AcceptChanges();
 
Share this answer
 
Ok now try below code. Its remove your value contain rows by founden from select method. Select method is return ICollection<datarow>

C#
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);
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900