窗体控件: comboBox1, comboBox2,listBox1,textBox1
数据库表字段:ID Name ParentID
private void Form1_Load( object sender, EventArgs e)
{ DataBindComboBox();
}
public void DataBindComboBox()
{
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = myDB.DatabindComboBox(string.Empty).Tables[0];
comboBox1.DataSource = bindingSource;
comboBox1.DisplayMember = "Name";
}
/**/
/// <summary>
/// 根据选择的省份获取省份的市
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void comboBox1_SelectedIndexChanged( object sender, EventArgs e)
{
string Condition = string.Format(" where ParentID in(select ID from DeptTree where Name='{0}')", comboBox1.Text);
DataTable dataTable=myDB.DatabindComboBox(Condition).Tables[0];
if (dataTable.Rows.Count > 0)
{
comboBox2.DataSource = dataTable;
comboBox2.DisplayMember = "Name";
listBox1.DataSource = dataTable;
listBox1.DisplayMember = "Name";
}
else
{
comboBox2.DataSource = null;
listBox1.DataSource = null;
}
}
private void listBox1_SelectedIndexChanged( object sender, EventArgs e)
{
textBox1.Text = listBox1.Text.ToString();
}
/// 根据选择的省份获取省份的市
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void comboBox1_SelectedIndexChanged( object sender, EventArgs e)
{
string Condition = string.Format(" where ParentID in(select ID from DeptTree where Name='{0}')", comboBox1.Text);
DataTable dataTable=myDB.DatabindComboBox(Condition).Tables[0];
if (dataTable.Rows.Count > 0)
{
comboBox2.DataSource = dataTable;
comboBox2.DisplayMember = "Name";
listBox1.DataSource = dataTable;
listBox1.DisplayMember = "Name";
}
else
{
comboBox2.DataSource = null;
listBox1.DataSource = null;
}
}
private void listBox1_SelectedIndexChanged( object sender, EventArgs e)
{
textBox1.Text = listBox1.Text.ToString();
}
/**/
/// <summary>
/// 把地名显示到控件中
/// </summary>
/// <returns></returns>
public DataSet DatabindComboBox( string Condition)
{
string sqlstr = "select Name from DeptTree" + Condition;
return RunSQL_DataTable(sqlstr);
}
/// 把地名显示到控件中
/// </summary>
/// <returns></returns>
public DataSet DatabindComboBox( string Condition)
{
string sqlstr = "select Name from DeptTree" + Condition;
return RunSQL_DataTable(sqlstr);
}