Vinit Patel

Dropdownlist show State with City

Posted on: October 3, 2009


DataRow[] results = null;
DataTable above24 = null;
DataTable dsStates = //state information
DataTable dsCity = //city information

ddlRegion.Items.Clear();
ddlRegion.Items.Insert(0, new OptionListItem(“Select Region…”, “-1”));

int cntr = 1;

for (int i = 0; i < dsStates.Rows.Count; i++)
{

ddlRegion.Items.Insert(cntr, new OptionGroupItem(dsStates.Rows[i][“State”].ToString()));
ddlRegion.Items[cntr].Attributes.Add(“style”, “FONT-WEIGHT: bold;BACKGROUND-COLOR: #ccccff;COLOR: black;FONT-SIZE: 1.5em”);

cntr++;

results = null;

//copy the schema of source table
above24 = dsCity.Clone();

//get only the rows you want
if (dsStates != null)
{
results = dsCity.Select(“StateID=” + Convert.ToInt32(dsStates.Rows[i][“StateID”].ToString()));
}

//populate new destination table
foreach (DataRow dr in results)
above24.ImportRow(dr);

for (int j = 0; j < above24.Rows.Count; j++)
{
ddlRegion.Items.Insert(cntr, new OptionListItem(above24.Rows[j][“CityName”].ToString(), above24.Rows[j][“CityName”].ToString()));
cntr++;
}
}

It will look like that.

1

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Calendar

October 2009
M T W T F S S
« Sep   Dec »
 1234
567891011
12131415161718
19202122232425
262728293031  

Stats Of Blog

  • 6,962 hits

Cluster Map

%d bloggers like this: