Reputation: 1597
I am trying to populate a combo box with data but I cant get it work
public void fillZone()
{
string sql = "select location from zone";
MySqlDataAdapter zonedapter = new MySqlDataAdapter(sql,conn);
DataTable dt = new DataTable("zone");
zonedapter.Fill(dt);
}
I use fillZone() to get data from mysql and have it stored in a dataTable.I want to populate a combobox named zonecb with data from returned result above, as shown below
this.zonecb.ItemsSource = "Location";
this.zonecb.DisplayMemberPath = "Location";
my xml is as shown:
<ComboBox Canvas.Left="91" Canvas.Top="57" Height="23" Name="zonecb" Width="200" ItemsSource="{Binding}" />
Any ideas?
Upvotes: 0
Views: 7266
Reputation: 708
you will need to acces dt, i dont know if you call your fillzone only for filling combobox but i would suggest:
public DataTable fillZone()
{
string sql = "select location from zone";
MySqlDataAdapter zonedapter = new MySqlDataAdapter(sql,conn);
DataTable dt = new DataTable("zone");
zonedapter.Fill(dt);
return dt;
}
public void fillcombo()
{
DataTable dt = fillZone();
foreach (DataCell cell in dt)
{
zonecb.add(cell.Value)
}
}
EDIT:
im using a same kind of method, but ith a postgreSql Database, by changing PgsqlDataAdapter to MySqlDataAdapter this might/should work
public DataSet GetInformation(string str)
{
ds = new DataSet("Tables");
Npgsql.NpgsqlDataAdapter da = new Npgsql.NpgsqlDataAdapter(str, connection);
da.TableMappings.Add("Table", "Program");
da.Fill(ds);
return ds;
}// send query to database, get table
String str is your Query.
public DataTable GetInfo()
{
string Query = "select location from zone";
DataSet Set = GetInformation(Query);
return Set.Tables[0];
}
now you got a table wih all the data you need/wanted.
DataTable DT = dBQuery.GetInfo();
for (int i = 0; i < result.Rows.Count; i++)
{
zonecd.Items.Add(result.Rows[i].ItemArray[0].ToString());
}
now you populated your combobox
Upvotes: 2