locked
Binding data from sqlite database to combo box in windows 8 apps RRS feed

  • Question

  • Hi

    How to bind the data to combobox control in windows 8 apps from sqlite database.

    (I had already stored the data into the database .i want one column data to be displayed into the combo box control when the page is loaded)


    amair

    Friday, June 21, 2013 7:18 AM

Answers

  • Hello Amair,

    Use Loaded event  instead of constructor. you are binding SelectedValuePath; it 'll not display anything. Use ItemTemplate for binding.


    see the proper work around at here

    If problem persist then share your workable sample


    Vote As Helpful, Propose As Answer, or Mark As Answer
    Microsoft Certified Solutions Developer
    My Samples



    Friday, June 21, 2013 7:58 AM
  • Your ComboBox doesn't have a ItemsContainerStyle. You can define like this:

    <Style x:Key="ComboBoxItemStyle" TargetType="ComboBoxItem">
        <Setter Property="Template">
             <Setter.Value>
                  <ControlTemplate TargetType="ComboBoxItem">
                       <TextBlock Text="{Binding Name}"/>
                  </ControlTemplate>
             </Setter.Value>
        </Setter>
    </Style>

    and use like this:

    <ComboBox ItemContainerStyle="{StaticResource ComboBoxItemStyle}">

    Friday, June 21, 2013 7:59 AM

All replies

  • You can query the database to get one column's datas, the result is a List<...>, and then set binding to ComboBox's Source.
    Friday, June 21, 2013 7:31 AM
  • public sealed partial class IncomeAdd : SmartDivaa.Common.LayoutAwarePage
        {

    IncomeViewModel income = new IncomeViewModel(); 

    List<IncomeViewModel.aa> a = new List<IncomeViewModel.aa>();

    public IncomeAdd()
            {
                this.InitializeComponent();
                a = income.getCombo();

    cba.datacontext=a;

                  }}

    IncomeViewModel.cs

    public List<aa> getCombo()
            {
                using (var db = new SQLite.SQLiteConnection(App.DBPath))
                {
                    var table1 = db.Table<bbb>().ToList();
                    List<aa> gg = new List<aa>();
                    for (int index = 0; index < table1.Count; index++)
                    {
                        aa data = new aa();
                        data.Name = table1[index].Name;
                        gg.Add(data);
                    }
                    return gg;
                }
            }

    Model.cs

          

    namespace SmartDivaa.Models
    {
      public   class bbb
        {

          public int Id
          {
              set;
              get;
          }
          public string Name
          {
              set;
              get;
          }
        }
    }

    View.cs

    <ComboBox   ItemsSource="{Binding}" SelectedValuePath="Name" x:Name="cba" HorizontalAlignment="Left" Height="35" Width="322.886" RenderTransformOrigin="0.5,0.5" UseLayoutRounding="False" d:LayoutRounding="Auto"/>

    The above is the code that i have written,but iam  not able to bind the data to combo box.

    in the variable "a" i will be getting the data but it is not binded to combobox control.please let me know the correction i  will be very thakful to u


    amair

    Friday, June 21, 2013 7:50 AM
  • Hello Amair,

    Use Loaded event  instead of constructor. you are binding SelectedValuePath; it 'll not display anything. Use ItemTemplate for binding.


    see the proper work around at here

    If problem persist then share your workable sample


    Vote As Helpful, Propose As Answer, or Mark As Answer
    Microsoft Certified Solutions Developer
    My Samples



    Friday, June 21, 2013 7:58 AM
  • Your ComboBox doesn't have a ItemsContainerStyle. You can define like this:

    <Style x:Key="ComboBoxItemStyle" TargetType="ComboBoxItem">
        <Setter Property="Template">
             <Setter.Value>
                  <ControlTemplate TargetType="ComboBoxItem">
                       <TextBlock Text="{Binding Name}"/>
                  </ControlTemplate>
             </Setter.Value>
        </Setter>
    </Style>

    and use like this:

    <ComboBox ItemContainerStyle="{StaticResource ComboBoxItemStyle}">

    Friday, June 21, 2013 7:59 AM
  • I had used the itemcontainerstyle in my project,the problem that iam going to face is iam not able to select the items in the combo box.it is just displaying the list of items

    amair

    Friday, June 21, 2013 9:23 AM
  • you can use ItemContainerStyle with ItemTemplate.

    SelectedValuePath is not for this instead it is used to get the selected value. 

    Please Upload a sample at somewhere or Skydrive, If you require further assistance.


    Vote As Helpful, Propose As Answer, or Mark As Answer
    Microsoft Certified Solutions Developer
    My Samples

    Friday, June 21, 2013 9:54 AM
  • Question
    You cannot vote on your own post
    0

    public sealed partial class IncomeAdd : SmartDivaa.Common.LayoutAwarePage
        {

    IncomeViewModel income = new IncomeViewModel(); 

    List<IncomeViewModel.aa> a = new List<IncomeViewModel.aa>();

    public IncomeAdd()
            {
                this.InitializeComponent();
              

                  }

    public void loadstate()

    {

      a = income.getCombo();

    cba.datacontext=a;

    }

    }

    IncomeViewModel.cs

    public List<aa> getCombo()
            {
                using (var db = new SQLite.SQLiteConnection(App.DBPath))
                {
                    var table1 = db.Table<bbb>().ToList();
                    List<aa> gg = new List<aa>();
                    for (int index = 0; index < table1.Count; index++)
                    {
                        aa data = new aa();
                        data.Name = table1[index].Name;
                        gg.Add(data);
                    }
                    return gg;
                }
            }

    Model.cs

          

    namespace SmartDivaa.Models
    {
      public   class bbb
        {

          public int Id
          {
              set;
              get;
          }
          public string Name
          {
              set;
              get;
          }
        }
    }

    View.cs

    <ComboBox   ItemsSource="{Binding}"  x:Name="cba"/>

    The above is the code that i have written,but iam  not able to bind the data to combo box.

    in the variable "a" i will be getting the data but it is not binded to combobox control.please let me know the correction i  will be very thakful to u

    please tell me the simple code so tat i can get data to combo box from database


    amair

    Friday, June 21, 2013 10:32 AM