Click here to Skip to main content
15,891,652 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wants get the out put from database into combo box,
retrieved data from the database but it's not bind into the combo box
all the values are stored into observable collection but it's not binding

here my Code


XAML
XML
<StackPanel Orientation="Horizontal" DataContext="{Binding Path=LocationListModel, Mode=TwoWay}"  >
                   <StackPanel Margin="5" >
                       <Label Content="Location Name"/>
                       <!--<TextBox TextWrapping="Wrap" Name="cmbLocation" Text="{Binding LocationNameLists, Mode=OneWay}" ></TextBox>-->
                       <ComboBox Name="cmbLocation"  Grid.Column="1" Grid.Row="1" Width="200" ItemsSource="{Binding LocationNameList}"  DisplayMemberPath="LocationName"  Loaded="cmbLocation_Loaded">
                           <!--<ComboBoxItem Content="qwerty"/>-->
                       </ComboBox>
                   </StackPanel>
                   <StackPanel Margin="5">
                       <Label Content="Building Name"/>
                       <ComboBox Name="cmbBuilding"  Width="200"/>
                   </StackPanel>
               </StackPanel>


WPF


public class LocationListModel : paymentModel
{
//LocationEntity locationEntity = new LocationEntity();

private ObservableCollection<paymententity> LocationList = new ObservableCollection<paymententity>();
// public ObservableCollection<paymententity> LocationId = new ObservableCollection<paymententity>();

public ObservableCollection<paymententity> LocationNameList
{
get { return LocationList; }

set
{
LocationList = value;

base.RaisePropertyChangedEvent("LocationNameList");
}
}

public ObservableCollection<paymententity> LocationIdList
{
get;
set;

}


public void locationListModel()
{

PaymentEntity paymentEntity = new PaymentEntity();
PaymentBussiness paymentBussienss = new PaymentBussiness();
DataTable LocationNameList = paymentBussienss.GetLocationList();

foreach (DataRow row in LocationNameList.Rows)
{
var obj = new PaymentEntity()
{
LocationId = (int)row.ItemArray[0],
LocationName = (string)row.ItemArray[1]

};

LocationList.Add(obj);

}
base.RaisePropertyChangedEvent("LocationNameList");

}

}
Posted

Replace Your combobox with following code

<combobox name="cmbLocation" grid.column="1" grid.row="1" width="200"></combobox>

If you are getting data in datlist then you can bind this data in combobox like that...
public void fillLocation()(Call this Method when you want to bind Location in combobox)
{


cmbLocation.DataContext = dtSource;(your data table name whatever it is)
cmbLocation.SelectedValuePath = "ID";
cmbLocation.DisplayMemberPath = "Name";
cmbLocation.UpdateLayout();
cmbLocation.SelectedValue = 0;
}


Happy Coding............
 
Share this answer
 
v4
The data context was not set correctly.
This example shows you how you can successfully bind to a ViewModel.

Besides:
1) A model cannot be used as data context.
Data context is always a ViewModel
2) It is not good practice to use code behind in MVVM
You should use Commands instead.

MainWindow.xaml:
XML
<Window x:Class="WpfApplication14.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication14"
        Title="MainWindow" Height="350" Width="525">
    
    <Window.DataContext>
        <local:ViewModel></local:ViewModel>
    </Window.DataContext>
        
    <Grid>
        <StackPanel Orientation="Horizontal">
            <StackPanel Margin="5" >
                <Label Content="Location Name"/>               
                <ComboBox Name="cmbLocation"  Grid.Column="1" Grid.Row="1" Width="200" ItemsSource="{Binding LocationNameList}">                    
                </ComboBox>
            </StackPanel>
            <StackPanel Margin="5">
                <Label Content="Building Name"/>
                <ComboBox Name="cmbBuilding"  Width="200"/>
            </StackPanel>
        </StackPanel>
    </Grid>
</Window>


ViewModel.cs
C#
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace WpfApplication14
{
    class ViewModel : INotifyPropertyChanged
    {
        [AttributeUsageAttribute(AttributeTargets.Parameter, Inherited = false)]
        public sealed class CallerMemberNameAttribute : Attribute { }

        public event PropertyChangedEventHandler PropertyChanged = delegate { };

        public void OnPropertyChanged([CallerMemberName]string propertyName = null)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private ObservableCollection<string> locationNameList;

        public ObservableCollection<string> LocationNameList
        {
            get
            {
                return locationNameList;
            }
            set
            {
                if (locationNameList != value)
                {
                    locationNameList = value;

                    OnPropertyChanged();
                }
            }
        }

        public ViewModel()
        {
            locationNameList = new ObservableCollection<string>();

            LocationNameList.Add("London");
            LocationNameList.Add("Berlin");
            LocationNameList.Add("Paris");
            LocationNameList.Add("Mumbai");
            LocationNameList.Add("Toronto");
        }
    }
}
 
Share this answer
 
v2
Comments
[no name] 18-Feb-15 9:20am    
But i have use the userControl not windows form in wpf,
and also i want get location Id and Location Name from database to future reference ,

how to bind the single value into combo box from locationEntity,,,,,,??????

u wants to know one thing i got the values bt it's not Bind
TheRealSteveJudge 18-Feb-15 9:50am    
What you showed us is only a fragment of your work.
My suggestion has nothing to do with Windows Forms.
It uses WPF resp. XAML.
Whether it is a user control or a window does not change anything.
The data binding mechanism is the same.

Maybe you can try it to get a feeling how to use data binding the MVVM way.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900