Wpf组合框没有填充sqlite数据

时间:2016-01-09 04:02:01

标签: c# wpf sqlite combobox

我有wpf组合框,我试图使用以下代码填写。我发现这些来自另一个堆栈的确切代码遵循Attach a SQL database to ComboBox.ItemSource (WPF)。没有任何东西填充到组合框中。如果我根据companyID提供一些值,我也想用什么代码来设置所选值。

        private void fillComboBox()
        {

            try
            {
                getCon getMyCon1 = new getCon();
                SQLiteConnection con = getMyCon1.GetConnection();
                SQLiteCommand sqlcmd = new SQLiteCommand();
                SQLiteDataAdapter sqladp = new SQLiteDataAdapter();
                DataSet ds = new DataSet();


                sqlcmd.Connection = con;
                sqlcmd.CommandType = CommandType.Text;
                sqlcmd.CommandText = "Select * from company";
                sqladp.SelectCommand = sqlcmd;
                sqladp.Fill(ds, "defaultTable");
                DataRow nRow = ds.Tables["defaultTable"].NewRow();
                nRow["companyName"] = "Select A Company";
                nRow["companyID"] = "-1";
                ds.Tables["defaultTable"].Rows.InsertAt(nRow, 0);
                companyName.DataContext = ds.Tables["defaultTable"].DefaultView;
                //
                companyName.DisplayMemberPath = ds.Tables["defaultTable"].Columns[0].ToString();
                companyName.SelectedValuePath = ds.Tables["defaultTable"].Columns[1].ToString();

            }
            catch (Exception expmsg)
            {

            }
            finally
            {
                //sqladp.Dispose();
                //sqlcmd.Dispose();
            }
   }

1 个答案:

答案 0 :(得分:1)

设置ItemsSource的{​​{1}},您现在只设置companyName

DataContext