I have wpf combo box which I am trying to fill using the following codes. I found these exact codes from another stack follow here Attach a SQL database to ComboBox.ItemSource (WPF). Nothing gets populate into the combo box. I also would like to what code can I use to set selected value if I provide some value based on the 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();
}
}
Aucun commentaire:
Enregistrer un commentaire