Help On Lambda Expression

Posted by Vijayar under C# on 6/28/2011 | Points: 10 | Views : 1394 | Status : [Member] | Replies : 2
Hi

I am making countries as bold and states as normal and binding them to a dropdown,to do this iam using lambda expression,But My problem is after selecting one county all other countries are losing boldness.But i want to retain that boldness.Please help me in this issue

private void SetDropDownStyle()
{
foreach (var item in ddlcounstates.Items.Cast<ListItem>().Where(li => li.Value.StartsWith("C", StringComparison.OrdinalIgnoreCase)))
{

item.Attributes.CssStyle.Add("font-weight", "bold");
}
}
and binding query is

Select 'C'+Cast(countryID as varchar)id , CountryID as CountryID,CountryName from Countries UNION Select 'S'+Cast(stateID as varchar)stateID ,CountryID,StateName from [States] order by CountryID,ID

vijaya


Responses

Posted by: Lokesh76 on: 6/28/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

As you put following line in your code
var item in ddlcounstates.Items.Cast<ListItem>().Where(li => li.Value.StartsWith("C", StringComparison.OrdinalIgnoreCase)
that's means in complete collection you put where condition and its return only first value to you rather then as per your requirement you need to return back a collection with all items of country data.

You can check in debug how many times your foreach loop excutes, make the loop on ddlcounstates.Items collection and check the condition inside the loop its work for you.


Thanks
Lokesh Kumar

Vijayar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Lokesh76 on: 6/28/2011 [Member] Starter | Points: 25

Up
0
Down
try like this

foreach (var item in ddlcounstates.Items)
{
//Check here that item value contains a country code
// If its country then
item.Attributes.CssStyle.Add("font-weight", "bold");
}

Thanks
Lokesh Kumar

Vijayar, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response