|
let's say i have two columns,one called area and second called city .i want to get all the cities when i choos a Specific area .
i use with datatable and i don't know how to Formulate an sql Query.it's suppose to be like that:
string areaName = DropDownList1.Items[DropDownList1.SelectedIndex].ToString().Trim();
DataRow[] city = dt.Select("city_name where area='"+areaName+"'");
for (int i = 0; i < city.Count<DataRow>(); i++)
{
DropDownList2.Items.Add(city[i]["city_name"].ToString());
}
|
|
|
|
|
I can't quite tell what you are trying to do there, but (dt is a DataTable, right?) you could try:
dt.DefaultView.RowFilter = "area=" + areaname ;
DropDownList2.DataSource = dt.DefaultView ;
Edit: Here's a full working example.
System.Data.DataRow dr ;
// Define a State table
System.Data.DataTable state = new System.Data.DataTable("State");
state.Columns.Add(new System.Data.DataColumn("ID",typeof(int)));
state.Columns.Add(new System.Data.DataColumn("Name",typeof(string)));
// Add some rows
dr = state.NewRow() ;
dr [ "ID" ] = 1 ;
dr [ "Name" ] = "Arizona" ;
state.Rows.Add ( dr ) ;
dr = state.NewRow() ;
dr [ "ID" ] = 2 ;
dr [ "Name" ] = "California" ;
state.Rows.Add ( dr ) ;
// Set the first ComboBox to display the table
this.comboBox1.DataSource = state.DefaultView ;
this.comboBox1.DisplayMember = "Name" ;
// Define a City table
System.Data.DataTable city = new System.Data.DataTable("State");
city.Columns.Add(new System.Data.DataColumn("ID",typeof(int)));
city.Columns.Add(new System.Data.DataColumn("State",typeof(int)));
city.Columns.Add(new System.Data.DataColumn("Name",typeof(string)));
// Add some rows
dr = city.NewRow() ;
dr [ "ID" ] = 1 ;
dr [ "State" ] = 1 ;
dr [ "Name" ] = "Phoenix" ;
city.Rows.Add ( dr ) ;
dr = city.NewRow() ;
dr [ "ID" ] = 2 ;
dr [ "State" ] = 1 ;
dr [ "Name" ] = "Tucson" ;
city.Rows.Add ( dr ) ;
dr = city.NewRow() ;
dr [ "ID" ] = 3 ;
dr [ "State" ] = 2 ;
dr [ "Name" ] = "San Diego" ;
city.Rows.Add ( dr ) ;
dr = city.NewRow() ;
dr [ "ID" ] = 4 ;
dr [ "State" ] = 2 ;
dr [ "Name" ] = "Los Angeles" ;
city.Rows.Add ( dr ) ;
// Set the second ComboBox to display the table
this.comboBox2.DataSource = city.DefaultView ;
this.comboBox2.DisplayMember = "Name" ;
// Add a SelectedIndexChanged handler
this.comboBox1.SelectedIndexChanged += delegate ( object sender , System.EventArgs e )
{
System.Data.DataRowView item = this.comboBox1.SelectedItem as System.Data.DataRowView ;
city.DefaultView.RowFilter = "State=" + item [ "ID" ] ;
return ;
} ;
// Select the first State
this.comboBox1.SelectedIndex = 0 ;
modified 14-Jan-13 9:49am.
|
|
|
|
|
i have two DropDownList and i want that if i choose from the first DropDownList valu(i mean area) automaticly in the second DropDownList i will have all the cities in that area.
|
|
|
|
|
Trap the change event on the 1st combo and filter the datasource of the second combo based on the selected item of the 1st combo.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
By learning what an event is, how to wire it up and how to deal with the result. All this depends on your UI, ASP/SL-WPF/Winforms are all different.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
someone already help me from anther site ,my problem now is only if i press on some button(even if the button do not doing anyting) i see the new data in my second DropDownList.how can i get the data Immediate in the second DropDownList when i change something in the first DropDownList ?
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList2.Items.Clear();
string areaName = DropDownList1.Items[DropDownList1.SelectedIndex].ToString().Trim();
DataRow[] city = dt.Select("area='" + areaName + "'");
for (int i = 0; i < city.Count<DataRow>(); i++)
{
DropDownList2.Items.Add(city[i]["city_name"].ToString());
}
}
|
|
|
|
|
Just set the DefaultView's RowFilter. Done.
modified 14-Jan-13 9:51am.
|
|
|
|
|
i dont understand to what do you mean
|
|
|
|
|
You have a DataTable with the cities, right? It has a DefaultView property. You set it as the DataSource of the control. Possibly have to set the DisplayMember property as well. And when the value in the other control changes, set the RowFilter value of this one.
|
|
|
|
|
the solution was to change in the propoty of the first DropDownList the autopostback to true
|
|
|
|