在同一个桌子上使用combobox1过滤组合框2 [英] Filter combobox 2 using combobox1 on same table
本文介绍了在同一个桌子上使用combobox1过滤组合框2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有2个组合框和数据网格视图我可以根据表分别过滤2个组合框,但我想基于第一个组合框过滤第二个组合框。我尝试了不同的方法,但我的第二个组合框是空的..没有任何反应..请帮助我。
字符串 Query = SELECT distinct [ t_street_name] FROM [ICPS]。[dbo]。[ticket];
SqlConnection conDataBase = new SqlConnection(conString);
SqlCommand cmdDataBase = new SqlCommand(Query,conDataBase);
SqlDataAdapter sda = new SqlDataAdapter(cmdDataBase);
SqlDataReader myReader;
尝试
{
conDataBase.Open();
myReader = cmdDataBase.ExecuteReader();
while (myReader.Read())
{
string t_street_name = myReader [ t_street_name]。ToString();
comboBox1.Items.Add(t_street_name);
}
}
catch (例外情况)
{
MessageBox.Show(ex.Message) ;
}
}
void fillcombo1()
{
< span class =code-sdkkeyword> String Query =( SELECT distinct [t_zone_name] FROM [ICPS ]。[dbo]。[ticket]其中[t_street_name] =' + comboBox1.SelectedItem + 'conString');
SqlConnection conDataBase = new SqlConnection(conString);
SqlCommand cmdDataBase = new SqlCommand(Query,conDataBase);
SqlDataReader myReader;
尝试
{
conDataBase.Open();
myReader = cmdDataBase.ExecuteReader();
while (myReader.Read())
{
string t_zone_name = myReader [ t_zone_name]。ToString();
comboBox2.Items.Add(t_zone_name);
}
}
catch (例外情况)
{
MessageBox.Show(ex.Message) ;
}
}
私有 void Form1_Load( object sender,EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged( object sender,EventArgs e)
{
}
private void button2_Click( object sender,EventArgs e)
{
SqlConnection conDatabase = new SqlConnection(constring);
conDatabase.Open();
DataTable db = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter( String .Format( 选择distinct * from + [ICPS]。[dbo]。[门票] +
其中[ ICPS]。[dbo]。[门票]。[t_street_name] =' + comboBox1.Text + ' +
和([ICPS]。[dbo]。[门票] 。[t_date_time_issued])BETWEEN转换(DATETIME,'{0}',103)和转换(DATETIME,'{1}',103),StartDate.Value.ToString( dd / MM / yyyy),EndDate.Value.ToString( dd / MM / yyyy)),constring);
sda.Fill(db);
dataGridView1.DataSource = db;
}
解决方案
真的很担心我想我问过一个疑难问题..帮助这个新手请专家......
I have 2 combo boxes and data grid view I can filter 2 combo boxes separately base on the table, but I want to filter second combobox based on 1st combo box. I tried different ways but my second combo box is empty.. nothing happens.. please help me with this.
String Query = " SELECT distinct [t_street_name] FROM [ICPS].[dbo].[tickets] ";
SqlConnection conDataBase = new SqlConnection(conString);
SqlCommand cmdDataBase = new SqlCommand(Query, conDataBase);
SqlDataAdapter sda = new SqlDataAdapter(cmdDataBase);
SqlDataReader myReader;
try
{
conDataBase.Open();
myReader = cmdDataBase.ExecuteReader();
while (myReader.Read())
{
string t_street_name = myReader["t_street_name"].ToString();
comboBox1.Items.Add(t_street_name);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void fillcombo1()
{
String Query = ("SELECT distinct [t_zone_name] FROM [ICPS].[dbo].[tickets] where [t_street_name] ='" + comboBox1.SelectedItem + "'conString ") ;
SqlConnection conDataBase = new SqlConnection(conString);
SqlCommand cmdDataBase = new SqlCommand(Query, conDataBase);
SqlDataReader myReader;
try
{
conDataBase.Open();
myReader = cmdDataBase.ExecuteReader();
while (myReader.Read())
{
string t_zone_name = myReader["t_zone_name"].ToString();
comboBox2.Items.Add(t_zone_name);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
SqlConnection conDatabase = new SqlConnection(constring);
conDatabase.Open();
DataTable db = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(String.Format("select distinct * from" + " [ICPS].[dbo].[tickets] " +
"where [ICPS].[dbo].[tickets].[t_street_name] = '" + comboBox1.Text + "'" +
"and ([ICPS].[dbo].[tickets].[t_date_time_issued]) BETWEEN Convert(DATETIME, '{0}', 103) AND Convert(DATETIME, '{1}', 103)", StartDate.Value.ToString("dd/MM/yyyy"), EndDate.Value.ToString("dd/MM/yyyy")), constring);
sda.Fill(db);
dataGridView1.DataSource = db;
}
解决方案
really worried I think I have asked a difficult question .. help this newbie please experts...
这篇关于在同一个桌子上使用combobox1过滤组合框2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文