Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
hi to all,
Can you tell how to set alternate colors in table.i create a table and to display in grid view but in my table i create many how to set color for one
send me code kindly help me quickly answer me
Posted

@model List<TestMVC4.Data.Country>

@{
    ViewBag.Title = "SearchView";
}

<h2>SearchView</h2>
 @{bool isalter = false;}
  @{string className = "";}
<table  style="width:60%">
<tr>
<th>Country ID</th>
<th>Country Name</th>
<th>View States</th>
</tr>
@foreach (var country in Model)
{
  if(isalter)
  {
      className="FirstRow";
      isalter=false;
  }
else
  {isalter=true;
      className="SecondRow"; 
  }
  <tr class=@className>
  <td>@country.CountryID</td>
  <td>@country.CountryName</td>
  <td>@Html.ActionLink("ViewStates", "redirect", new {countryCode=country.CountryID })</td>
</tr>
    }
</table>
 
Share this answer
 
Comments
Kornfeld Eliyahu Peter 8-Jul-14 9:14am    
As a newbie you may be surprised that no code need for that. It can be solved in CSS alone...
tr:nth-child(even) {background: #CCC}
tr:nth-child(odd) {background: #FFF}
JOTHI KUMAR Member 10918227 9-Jul-14 1:29am    
sorry peter it cant apply bcz in one table i have three <tr> tags so oly can u send code for one single <tr> tag
Kornfeld Eliyahu Peter 9-Jul-14 11:39am    
Your comment make no sense at all. You have to do some effort and learn the meaning of nth-child selector...
 
Share this answer
 
 
Share this answer
 
Comments
JOTHI KUMAR Member 10918227 8-Jul-14 8:48am    
i want to set one <tr> in my table there are three <tr> tags in my table if i set color apply in all <tr> tages so how to set one <tr> tags
Kornfeld Eliyahu Peter 8-Jul-14 8:56am    
Read the top 5 articles that, that search returns for you...
jo.him1988 8-Jul-14 9:02am    
ok i am newbie in mvc here is the answer after try on my project its bang to target :)

as simple logic
you have to create two css class as i made here FirstRow and SecondRow now declare one boolvariable to check alter row and one string variable for set class name at runtime
now on foreach loop check bool value and set tr class name

thats it


happy coding :) :) :)
JOTHI KUMAR Member 10918227 9-Jul-14 1:27am    
thanks jo for the reply can you send example for this
jo.him1988 9-Jul-14 2:03am    
please see above solution 2
when your fill your grid by for each loop only set <tr class=@className>

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