Click here to Skip to main content
15,919,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Binding database id,name to combobox using DisplayMemberPath and selectedMemberPath but i couldnt get how to access the id value. below is the code am using.what is wrong that am doing and to rectify it get id value upon selectionchanged.

person class
C#
public class Person
{
public int Uid{get; set;}
public string UName{get; set;}
public Person(int id,string name)
{
Uid=id;
UName=name;
}
}

internal IEnumerable<Person> GetDataForComboBox(string spName)
        {
            return CommonToAll(spName).AsEnumerable().Select(row =>
            {
                return new Person
                {
                   Uid = Convert.ToInt32(row["UserID"]),
                    UName= Convert.ToString(row["UserName"]),
                };
            });

        }

var transData = objDC.GetDataForComboBox("spGetAllData");
 observallableCollection<Person> data=new  observallableCollection<Person>();
  cmbTransporter.Items.Add(new Person(0,"-Select Any- "));
                    foreach (var item in transData)
                    {
                        cmbTransporter.Items.Add(new Person(item.Uid,item.UName));
                    }
               cmbTransporter.ItemsSource=data;
               cmbTTCTransporter.SelectedIndex = 0;


In Xaml
XML
<Combobox Name="cmbTransporter" selectedValuePath="{ Binding Person}" DisplayMemberPath="UName" SelectedValue="Uid"/>


In the above case,able to bind but nt bale to access id ??? how can get id by using above code.
Every time i cannot use combobox selected index because suppose if i delete any item in combobox all my code will not run as i required.Instead of saving selected index want to use selected value that means id which i want to bind whie saving to datatbase.

where i went wrong let me know plsss.
Posted

1 solution

your binding in xaml is wrong....please check the below

XML
<combobox name="cmbTransporter" selectedvaluepath="Uid" displaymemberpath="UName" selectedvalue="{ Binding Person}" />
code

by using cmbTransporter.SelectedValue.Tostring(), you will get value of id.

Hope it works fine now..check it
 
Share this answer
 
Comments
kida atlantis 20-Nov-13 23:35pm    
Thanks , its working now.
keerth516 20-Nov-13 23:56pm    
your welcome

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