如何填充第二个组合框。我在WPF C#中使用Sqlite [英] How do I populate the second combobox. I am using Sqlite in WPF C#
本文介绍了如何填充第二个组合框。我在WPF C#中使用Sqlite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好的数据显示在第一个comboBox上,但当我使用基于comboBox2的selectedValue的select语句时,第二个没有任何内容。我需要做些什么来解决这个问题。
谢谢
有一个好的。
Raj
Ok the data shows up on the First comboBox, but nothing on the second when I used the select statement based on the selectedValue of comboBox2. What do I need to do here to fix this.
Thanks
Have a good one.
Raj
private void List_DistrictName()
{
const string filename = @"C:\..\..SqliteDataFile_WithCode.db";
;
var conn = new SQLiteConnection("Data Source=" + filename + ";Version=3;");
SQLiteCommand sqlcmd = new SQLiteCommand();
sqlcmd.Connection = conn;
sqlcmd.CommandType = CommandType.Text;
sqlcmd.CommandText = ("select DIST_CODE, DTNAME_ENGLISH from GramPanch_MAPPING_VIl GROUP By DTNAME_ENGLISH");
DataSet ds = new DataSet();
var sqliteDa = new SQLiteDataAdapter();
sqliteDa.SelectCommand = sqlcmd;
try
{
sqliteDa.Fill(ds, "GramPanch_MAPPING_VIl");
DataRow nRow = ds.Tables["GramPanch_MAPPING_VIl"].NewRow();
nRow["DTNAME_ENGLISH"] = "District All";
nRow["DIST_CODE"] = "0";
ds.Tables["GramPanch_MAPPING_VIl"].Rows.InsertAt(nRow, 0);
//Binding the data to the combobox.
comBox1.DataContext = ds.Tables["GramPanch_MAPPING_VIl"].DefaultView;
//To display DISTNAME (DisplayMember in Visual Studio 2012)
comBox1.DisplayMemberPath = ds.Tables["GramPanch_MAPPING_VIl"].Columns["DTNAME_ENGLISH"].ToString();
comBox1.SelectedItem = ds.Tables["GramPanch_MAPPING_VIl"].Columns["DIST_CENSUS_CD_2011"].ToString();
//To store the DIST ID as hidden (ValueMember in Visual Studio 2012)
//comBox1.SelectedValuePath = ds.Tables["DTNAME"].Columns["DIST_CODE"].ToString();
//comBox1.SelectedValue = ds.Tables["GramPanch_MAPPING_VIl"].Columns["DIST_CODE"].ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
MessageBox.Show("An error occurred while loading District Name.");
}
finally
{
sqliteDa.Dispose();
sqlcmd.Dispose();
conn.Dispose();
}
}
#endregion
#region List Sub DistrictName for the ComBox2
private void List_SubDistrictName()
{
comBox2.Items.Clear();
const string filename = @"C:\..\..SqliteDataFile_WithCode.db";
var conn = new SQLiteConnection("Data Source=" + filename + ";Version=3;");
SQLiteCommand sqlcmd = new SQLiteCommand();
sqlcmd.Connection = conn;
sqlcmd.CommandType = CommandType.Text;
sqlcmd.CommandText = "select SB_DIST_NAME_ENGLISH, SB_DIST_ID from GramPanch_MAPPING_VIl WHERE DIST_CODE ='" + comBox1.SelectedValue+"'";
DataSet ds = new DataSet();
var sqliteDa = new SQLiteDataAdapter();
sqliteDa.SelectCommand = sqlcmd;
try
{
sqliteDa.Fill(ds, "GramPanch_MAPPING_VIl");
DataRow nRow = ds.Tables["GramPanch_MAPPING_VIl"].NewRow();
nRow["SB_DIST_NAME_ENGLISH"] = "--Please Select the SUB District--";
nRow["SB_DIST_ID"] = "0";
ds.Tables["GramPanch_MAPPING_VIl"].Rows.InsertAt(nRow, 0);
//Binding the data to the combobox.
comBox2.DataContext = ds.Tables["GramPanch_MAPPING_VIl"].DefaultView;
//To display Sub District name (DisplayMember in Visual Studio 2012)
comBox2.DisplayMemberPath = ds.Tables["GramPanch_MAPPING_VIl"].Columns["SB_DIST_NAME_ENGLISH"].ToString();
//To store the ID as hidden (ValueMember in Visual Studio 2012)
comBox2.SelectedValuePath = ds.Tables["GramPanch_MAPPING_VIl"].Columns["SB_DIST_ID"].ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
MessageBox.Show("An error occurred while loading Sub District Name.");
}
finally
{
sqliteDa.Dispose();
sqlcmd.Dispose();
conn.Dispose();
}
}
#endregion
private void OnLoad(object sender, RoutedEventArgs e)
{
List_DistrictName();
List_SubDistrictName();
}
这是XAML中ComboBox代码的一部分。\
我有
SelectedIndex =0ItemsSource ={Binding}
comboBox。
谢谢!!
Here is the part of the ComboBox code in the XAML.\
I have the
SelectedIndex="0" ItemsSource="{Binding}"
for both the comboBox.
Thanks!!
推荐答案
我设法通过使用MVVM解决问题。
感谢阅读。如果有人需要帮助,请告诉我,
I manage to work a way out by using MVVM.
Thanks to reading. If anyone needs help on this let me know,
这篇关于如何填充第二个组合框。我在WPF C#中使用Sqlite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文