C#winform省市县联动,以及有的县是空值时显示异常的处理

时间:2022-10-10 12:09:56

一、如下comboBox1、comboBox2、comboBox3,原来这三个都是空的,

C#winform省市县联动,以及有的县是空值时显示异常的处理

将数据库中的省份传递到comboBox1中

我的数据库有parent字段,根据市的parent找到省,根据县的找到市,所以下面的sql语句在省市县联动时,sql语句是一样的

C#winform省市县联动,以及有的县是空值时显示异常的处理

二、代码

1、在Load方法中,加载省份(根据需求变换sql语句)

 private void Form1_Load(object sender, EventArgs e)
{ using (SQLiteConnection con = new SQLiteConnection(Constants.DATA_SOURCE))
{
con.Open();
using (SQLiteCommand cmd = new SQLiteCommand())
{
cmd.Connection = con;
cmd.CommandText = string.Format("select * from test t where t.[district_level]=2");
int rows = cmd.ExecuteNonQuery();
SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "name";
comboBox1.ValueMember="id";
}
} }

(1)、用sql命令查询要在comboBox中显示的值

(2)、将值给DataTable

(3)、DisplayMember 就是要显示的数据库中的字段:name

(4)、ValueMember:一般是数据库中的主键:id

2、进入comboBox1事件,根据省份,显示市

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{ if (comboBox1.SelectedIndex > -)
{
DataRowView drv = (DataRowView)comboBox1.SelectedItem;
string id = drv.Row["id"].ToString();//获得已绑定的选项的id using (SQLiteConnection con = new SQLiteConnection(Constants.DATA_SOURCE))
{
con.Open();
using (SQLiteCommand cmd = new SQLiteCommand())
{
cmd.Connection = con;
cmd.CommandText = string.Format("select * from test t where t.[parent]='" + id + "'");
int rows = cmd.ExecuteNonQuery();
SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
comboBox2.DataSource = dt;
comboBox2.DisplayMember = "name";
comboBox2.ValueMember = "id";
}
} } }

3、进入comboBox2事件,根据市,显示县

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{ if (comboBox2.SelectedIndex > -)
{
DataRowView drv = (DataRowView)comboBox2.SelectedItem;
string id = drv.Row["id"].ToString();//获得id using (SQLiteConnection con = new SQLiteConnection(Constants.DATA_SOURCE))
{
con.Open();
using (SQLiteCommand cmd = new SQLiteCommand())
{
cmd.Connection = con;
cmd.CommandText = string.Format("select * from test t where t.[parent]='" + id + "'");
int rows = cmd.ExecuteNonQuery();
SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
comboBox3.DataSource = dt;
comboBox3.DisplayMember = "name";
comboBox3.ValueMember = "id";
}
} } }

三、 结果:

C#winform省市县联动,以及有的县是空值时显示异常的处理

四 、有的省没有县,县是空值时,联动会出现异常,上一次联动的县的值还在显示,

C#winform省市县联动,以及有的县是空值时显示异常的处理

点击县时,因为县的下一级没有值,显示的还是上次联动的结果,其实它并没有处于选中状态,只是显示出来了

C#winform省市县联动,以及有的县是空值时显示异常的处理

所以,处理一下,因为没有处于选中状态,所以SelectedIndex是-1

if(comboBox3.SelectedIndex!=)
{
comboBox3.Text = "";
}

五、其他:

1、如果一个页面有多条这样的省市县联动

C#winform省市县联动,以及有的县是空值时显示异常的处理

C#winform省市县联动,以及有的县是空值时显示异常的处理

把上面的步骤再来一遍即可,只是Load里需要注意,不能图省事把comboBox4直接加在comboBox1后,否则点下面的省份,上面的也会变

using (SQLiteConnection con = new SQLiteConnection(Constants.DATA_SOURCE))
{
con.Open();
using (SQLiteCommand cmd = new SQLiteCommand())
{
cmd.Connection = con;
cmd.CommandText = string.Format("select * from test where t.[district_level]=2");
int rows = cmd.ExecuteNonQuery();
SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "name";
comboBox1.ValueMember="id"; }
} using (SQLiteConnection con = new SQLiteConnection(Constants.DATA_SOURCE))
{
con.Open();
using (SQLiteCommand cmd = new SQLiteCommand())
{
cmd.Connection = con;
cmd.CommandText = string.Format("select * from test t where t.[district_level]=2");
int rows = cmd.ExecuteNonQuery();
SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt); comboBox4.DataSource = dt;
comboBox4.DisplayMember = "name";
comboBox4.ValueMember = "id";
}
}

2、把comboBox1的事件代码封装成一个类DBDao.cs,方法名为getAddress()

(1)sql语句用的一样的时候(上面说的,根据市的parent找到省,根据县的parent找到市),我把sql语句也封装了

public static void getAddress(ComboBox combo1, ComboBox combo2)
{
if (combo1.SelectedIndex > -)
{
DataRowView drv = (DataRowView)combo1.SelectedItem;
string id = drv.Row["id"].ToString();//获得id
using (SQLiteConnection con = new SQLiteConnection(Constants.DATA_SOURCE))
{
con.Open();
using (SQLiteCommand cmd = new SQLiteCommand())
{
cmd.Connection = con;
cmd.CommandText = string.Format("select t.* from test t where t.[parent]='" + id + "'");
int rows = cmd.ExecuteNonQuery();
SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
combo2.DataSource = dt;
combo2.DisplayMember = "name";
combo2.ValueMember = "id"; }
}
}
}

在comboBox1的点击事件中直接调用即可,

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
DBDao.getAddress(comboBox1, comboBox2);
} private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
DBDao.getAddress(comboBox2, comboBox3);
}

(2)sql语句不同的时候,不要把sql语句封装(留着以后用)

public static void ComboBoxLoad(string sql, ComboBox combobox)
{
using (SQLiteConnection con = new SQLiteConnection(Constants.DATA_SOURCE))
{
con.Open();
using (SQLiteCommand cmd = new SQLiteCommand())
{
cmd.Connection = con;
cmd.CommandText = string.Format(sql);
int rows = cmd.ExecuteNonQuery();
SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt); combobox.DataSource = dt;
combobox.DisplayMember = "name";
combobox.ValueMember = "id"; }
} }

其他页面调用:

string sql="select * from test t where t.id='0302' order by t.code asc";
DBDao.ComboBoxLoad(sql,comboBox12);

参考:

https://zhidao.baidu.com/question/242563101.html?qbl=relate_question_2&word=c%20combobox%BC%D3%D4%D8%CA%FD%BE%DD&skiptype=2