How to iterative through an table using foreach condition in C#
I have an table. I need to iterate,
for each(datarow r in datatable.rows){
foreach(datacolumns c in datatable.columns){
if() // nee开发者_如何学JAVAd to write an condition
{
// apply css
}
else
{
//no need to apply css
}
}
}
I have an column as Menu_ID if MEnu_ID as an value "7" the dnt apply the css how to write an condition here.
If I understood correctly, you need something like this:
foreach(datarow r in datatable.rows)
{
if(((int)r["Menu_ID"])==7) {
//Don't apply CSS
} else {
//Apply CSS
}
}
this is assuming your Menu_ID column is numeric. If it is a string, change to:
if(((string)r["Menu_ID"])=="7")
for(int i=0; i<= datatable.rows.count-1; i++)
{
if(datatable.Rows[i]["ColName"] == "1")
{
//do something
}
else
{
//do something
}
}
精彩评论