Formatting dropdown list

Posted by Vijayar under ASP.NET on 5/9/2011 | Points: 10 | Views : 3633 | Status : [Member] | Replies : 35
Hi
I have one dropdown list box,i am binding values from the database from 2 tables states and cities ,is it possible to show some values in bold and other values normally,help me on this issue it's urgent
The information in dropdownlooks like
Country1 ex: India

state1 AP
state2 Karnataka
Country2 USA
state1 Mexico
state2
I want only country name to be in bold

vijaya


Responses

Posted by: Ndebata on: 5/9/2011 [Member] Starter | Points: 25

Up
0
Down
Hi
Take a look at your previous post on this, just now i replied to it.
http://www.dotnetfunda.com/forums/thread4550-formatting-dropdown-list.aspx
Thanks,
Debata

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

Posted by: Vijayar on: 5/9/2011 [Member] Starter | Points: 25

Up
0
Down
Hi
I am binding values from database ,we cannot check with a value in a loop and we dont know the exact names of country names

vijaya

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

Posted by: Vijayar on: 5/9/2011 [Member] Starter | Points: 25

Up
0
Down
Hi
I am binding values from database ,we cannot check with a value in a loop and we dont know the exact names of country names

vijaya

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

Posted by: Ndebata on: 5/9/2011 [Member] Starter | Points: 25

Up
0
Down
Hi
How do you differentiate whether its a country or state?
Thanks,
Debata

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

Posted by: Vijayar on: 5/9/2011 [Member] Starter | Points: 25

Up
0
Down
Hi
I have one dropdown list box,i want to bind both county names and states manes into the dropdown from 2 different tables. I want only country name to be in bold..Help me in this issue

Country1 ex: India

state1 AP
state2 Karnataka
Country2 USA
state1 Mexico
state2
I want only country name to be in bold

vijaya

vijaya

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

Posted by: Vijayar on: 5/9/2011 [Member] Starter | Points: 25

Up
0
Down
Hi
I have one dropdown list box,i want to bind both county names and states manes into the dropdown from 2 different tables. I want only country name to be in bold..Help me in this issue

Country1 ex: India

state1 AP
state2 Karnataka
Country2 USA
state1 Mexico
state2
I want only country name to be in bold

vijaya

vijaya

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

Posted by: Ndebata on: 5/9/2011 [Member] Starter | Points: 25

Up
0
Down
Lets say two tables are as follows
1.Country

ID(uniqueidentifier) Name(nvarchar(50)
64ed1e8b-0664-4074-9d66-d5a5d6155feb INDIA
9584fece-e459-4c88-8f74-47a974996f5c USA

2.State

ID Name CountryID
4DEB1A74-D4A0-4ADF-827F-A6E2EB50B09A Orissa 64ED1E8B-0664-4074-9D66-D5A5D6155FEB
9F89361A-319B-4398-BE94-912F25DCDC0C Karnataka 64ED1E8B-0664-4074-9D66-D5A5D6155FEB
25CD4135-6D4C-459A-A7A5-DAB3069E84E7 NewYork 9584FECE-E459-4C88-8F74-47A974996F5C
6E617BE7-DBD6-41F2-BDD6-C147C297634A Mexico 9584FECE-E459-4C88-8F74-47A974996F5C

Use the following query to populate a datatable named dt and bind it to the dropdown list.

Select ID,Name,ID as CountryID,0 [IsState] from Country
Union
Select ID,Name,CountryID,1 [IsState] from [State]
Order by
CountryID,[IsState],Name

Bind the data to dropdownlist

DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "ID";
DropDownList1.DataBind();

Then for each item check IsState value and change the color

foreach (ListItem item in DropDownList1.Items)
{
if(dt.Rows.Cast<DataRow>().Where(r=>r["ID"].Equals(new Guid(item.Value))).FirstOrDefault()["IsState"].Equals("0"))
item.Attributes.CssStyle.Add("color", "red");
}


Thanks,
Debata

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

Posted by: Vijayar on: 5/9/2011 [Member] Starter | Points: 25

Up
0
Down
Hi
But the id in my table is a 2 digit number,i am getting error when using the code as

Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).Help me

vijaya

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

Posted by: Vijayar on: 5/9/2011 [Member] Starter | Points: 25

Up
0
Down
Hi
When i click on save button country id should be saved in country table,state id into state table .how to do this using spilt function.example with ex

vijaya

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

Posted by: Ndebata on: 5/9/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Vijay,
As you are binding both country and state to same dropdown list which has two property called Value and Text,
The Value should be unique to uniquely identify an item in dropdown list, The best way to achievie this by using uniqueidentifier
, else you can use some convention like put a character "C" before country id and "S" before state id so that there will be distinct IDs for each dropdownlist item.

In my previous post i used uniqueidentifier in SQL and GUID in C#.
Now I am going to describe how you can use some Convention in case of ID defined as integer in database.
Change your sql query used to load the datatable to this

Select 'C'+Cast(ID as varchar) ID,Name,ID as CountryID from tblCountry
Union
Select 'S'+Cast(ID as varchar) ID,Name,CountryID from tblState
Order by
CountryID,ID,Name

Then bind the dropdownlist and change the color of those having ID starts with "C"

DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "ID";
DropDownList1.DataBind();

foreach (var item in DropDownList1.Items.Cast<ListItem>().Where(li=>li.Value.StartsWith("C",StringComparison.OrdinalIgnoreCase)))
{
item.Attributes.CssStyle.Add("color", "red");
}

Thanks,
Debata

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

Posted by: Vijayar on: 5/10/2011 [Member] Starter | Points: 25

Up
0
Down
Hi
In backend i am getting the result, but in front end by using the loop i am not getting bold or colors applied for countries,should i use sepaarte fuction to write the loop ,inform me

vijaya

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

Posted by: Ndebata on: 5/10/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Vijay,
Just change the sql query and write down the code in code behind file where you are binding the dropdownlist,
or Post your code.

Thanks,
Debata

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

Posted by: Vijayar on: 5/10/2011 [Member] Starter | Points: 25

Up
0
Down
private void bindtoddl()
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings[""].ToString();
conn.Open();


SqlDataAdapter da = new SqlDataAdapter("Select 'S'+Cast(stateID as varchar)stateID ,CountryID,StateName from [States] UNION Select 'C'+Cast(countryID as varchar)Countryid , CountryID as CountryID,CountryName from Countries order by CountryID,stateID", conn);

DataSet ds = new DataSet();
da.Fill(ds);

DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "StateName";
DropDownList1.DataValueField = "CountryID";
DropDownList1.DataBind();



}

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
foreach (var item in DropDownList1.Items.Cast<ListItem>().Where(li => li.Value.StartsWith("C", StringComparison.OrdinalIgnoreCase)))
{

item.Attributes.CssStyle.Add("style", "font-weight:bold");

}



This is my code,Please Check it


bindtoddl();


}

}






page load



vijaya

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

Posted by: Ndebata on: 5/10/2011 [Member] Starter | Points: 25

Up
0
Down
Hi
You have to load the dropdown first then apply formatting
change your page load like this

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindtoddl();
foreach (var item in DropDownList1.Items.Cast<ListItem>().Where(li => li.Value.StartsWith("C", StringComparison.OrdinalIgnoreCase)))
{
item.Attributes.CssStyle.Add("color", "red");
}
}
}


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

Posted by: Vijayar on: 5/10/2011 [Member] Starter | Points: 25

Up
0
Down
Hi
i have tried like this also but not working

vijaya

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

Posted by: Vijayar on: 5/11/2011 [Member] Starter | Points: 25

Up
0
Down
Hi
Please help me to make country names in bold and state names normal,where should i change

vijaya

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

Posted by: Ndebata on: 5/11/2011 [Member] Starter | Points: 25

Up
0
Down
Hi

Update your code

private void bindtoddl()
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings[""].ToString();
conn.Open();
SqlDataAdapter da = new SqlDataAdapter("Select 'S'+Cast(stateID as varchar) ID ,CountryID,StateName as Name from [States] UNION Select 'C'+Cast(countryID as varchar) ID , CountryID as CountryID,CountryName as Name from Countries order by CountryID,ID
", conn);
DataSet ds = new DataSet();
da.Fill(ds);
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "Name
";
DropDownList1.DataValueField = "ID
";
DropDownList1.DataBind();
}

Then in page load
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindtoddl();

foreach (var item in DropDownList1.Items.Cast<ListItem>().Where(li => li.Value.StartsWith("C", StringComparison.OrdinalIgnoreCase)))
{
item.Attributes.CssStyle.Add("color", "red");
}
}
}





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

Posted by: Vijayar on: 5/11/2011 [Member] Starter | Points: 25

Up
0
Down
First I tried like this only,but i am not getting the output.i tried on DropDownList1_DataBound event also but not getting,suggest me any other way ,help me in this issue

vijaya

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

Posted by: Ndebata on: 5/11/2011 [Member] Starter | Points: 25

Up
0
Down
your query was wrong, aslo you are binding value field to CountryID which should be ID.
This is working,
*Please note that in IE font is not rendering correctly, so try to use color property

And try to bind the list first the change its color.
Thanks,
Debata

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

Posted by: Vijayar on: 5/11/2011 [Member] Starter | Points: 25

Up
0
Down
Hi

if the query was wrong i should not get the result as desired i.e
Country1
State1
state2
country2
State1


But i am the result,i dont want color is not getting applied .I want capital name in bold only i tried like this
foreach (var item in DropDownList1.Items.Cast<ListItem>().Where(li => li.Value.StartsWith("C", StringComparison.OrdinalIgnoreCase)))
{
item.Attributes.CssStyle.Add("style", "font-weight:bold");
}
but not getting in blod


vijaya

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

Posted by: Ndebata on: 5/11/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Vijay,
Please change the code as per the highlighted
above in blue in previous post, In fire fox you can notice the font weight change.
Please take a look at the attached

Thanks,
Debata
 Download source file

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

Posted by: Vijayar on: 5/11/2011 [Member] Starter | Points: 25

Up
0
Down
Hi

your attachment is working fine ,By in my program i am not getting .I will send my tables structure.please check whether my query is correct or not

Country
CREATE TABLE [Countries] (
[CountryId] int IDENTITY(1, 1) NOT NULL,
[CountryName] varchar(50) NULL)

ON [PRIMARY];

Query is
Select " 'S'+Cast(stateID as varchar)stateID ,CountryID,StateName from [States] UNION Select 'C'+Cast(countryID as varchar)Countryid , CountryID as CountryID,CountryName from Countries order by CountryID,stateID"
please let me know any mistake present.



CREATE TABLE [States] (
[StateId] int IDENTITY(1, 1) NOT NULL,
[CountryId] int NOT NULL,
[StateName] varchar(50) NULL)

ON [PRIMARY];
GO


and the query is







vijaya

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

Posted by: Vijayar on: 5/11/2011 [Member] Starter | Points: 25

Up
0
Down
Hi
Thanks alot for ur suggestions,ur suggestions helped alot to solve the query

vijaya

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

Posted by: Ndebata on: 5/11/2011 [Member] Starter | Points: 25

Up
0
Down
Your problem is sorted out or not.
Thanks,
Debata

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

Posted by: Vijayar on: 5/12/2011 [Member] Starter | Points: 25

Up
0
Down
Hi
The problem got sorted out.Thank u very much for responding to my queries

vijaya

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

Posted by: Vijayar on: 5/12/2011 [Member] Starter | Points: 25

Up
0
Down
Hi
I have a new requirement with the same form ,when i select the state name of a particular country,in dropdown both country and state should be displayed.I addes 3 more fields and when i click on save button both state id and country id should be saved into the table
ex
India
Ap
Karnataka
when i select karnataka in drop down India-Karnataka should be displayed .explain me using spilt function to save the id's into table

vijaya

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

Posted by: Vijayar on: 5/12/2011 [Member] Starter | Points: 25

Up
0
Down
Hi
i tried our code,i am getting values but string format holds null value

vijaya

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

Posted by: Vijayar on: 5/12/2011 [Member] Starter | Points: 25

Up
0
Down
Help me how to split the values available in dropdown.i-e India-Karnataka
i need to get their ids separately into a variable

vijaya

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

Posted by: Ndebata on: 5/12/2011 [Member] Starter | Points: 25

Up
0
Down
Hi

From the selected value of the dropdown check whether it is a state or country, if item value is started with S then go to the database and find the CountryID for the StateID ( Which will get after replacing "S" by Empty string.

Thanks,
Debata

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

Posted by: Vijayar on: 5/12/2011 [Member] Starter | Points: 25

Up
0
Down
Hi

if possible send me an example code on split functionality

vijaya

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

Posted by: Ndebata on: 5/12/2011 [Member] Starter | Points: 25

Up
0
Down
PFA sample code.
 Download source file

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

Posted by: Vijayar on: 5/12/2011 [Member] Starter | Points: 25

Up
0
Down
Hi
if u r free please explain my problem with these 2 tables structures from starting .i.e. binding both country and state to a drop down,if we select country it should display both country and statte ,on button click id's should be saved in to table .this would be a great help for me .

Country
CREATE TABLE [Countries] (
[CountryId] int IDENTITY(1, 1) NOT NULL,
[CountryName] varchar(50) NULL)







CREATE TABLE [States] (
[StateId] int IDENTITY(1, 1) NOT NULL,
[CountryId] int NOT NULL,
[StateName] varchar(50) NULL)











vijaya

vijaya

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

Posted by: Vijayar on: 5/13/2011 [Member] Starter | Points: 25

Up
0
Down
HI

u have taken 2 tables with this structures,i have a small doubt what is the id field represent whether it is state id or country id .please inform me.it's urgent.
Country

ID Name

1 INDIA

2 USA




State

ID Name CountryID

1 Orissa 1

2 Karnataka 1

3 NewYork 2

4 Mexico 2

vijaya

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

Posted by: Vijayar on: 5/13/2011 [Member] Starter | Points: 25

Up
0
Down
u r showing combination of capital and state in a label but not in dropdown,so that u can compare the list value whether it starts with s ,but iam showing in dropdown ,i am not getting their values ,only getting text ads India-Karnataka
in value also.how to get rid of this..please suggest me.i am writting split function as,is this correct or suggest other way
string s = DropDownList1.SelectedItem.Value.ToString();
string[] strspilt = s.Split('-');
string country = strspilt[0].ToString();


string state = strspilt[1].ToString();
cmd.Parameters.AddWithValue("@CountryId", Convert.ToInt32(country));
cmd.Parameters.AddWithValue("@StateId", Convert.ToInt32(stateid));

vijaya

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

Posted by: Vijayar on: 5/16/2011 [Member] Starter | Points: 25

Up
0
Down
HI

u have taken 2 tables with this structures,i have a small doubt what is the id field represent whether it is state id or country id .please inform me.it's urgent.Please clarify this doubt .it's very urgent to complete my task
Country

ID Name

1 INDIA

2 USA




State

ID Name CountryID

1 Orissa 1

2 Karnataka 1

3 NewYork 2

4 Mexico 2

vijaya

vijaya

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

Login to post response