Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've a DataGrid with a manually added combobox column that will have 2 values Yes/No. How do I assign the value 1 = Yes and 2 = No to this combobox?
Posted
Comments
Aniket Yadav 9-Mar-12 8:10am    
Elaborate more or provide a sample code
pmcm 9-Mar-12 8:17am    
the DGV is bound to the following:
<pre lang="c#">
public ObjectResult GetCompanyExecutivesList()
{
// Check we have an ObjectContext
if (entities == null) entities = new CompanySecretaryEntities();

//define the query
var query = from ce in entities.CompanyExecutives
join exec in entities.Executives on ce.ExecutiveID equals exec.ExecutiveID
join companies in entities.Companies on ce.CompanyID equals companies.CompanyID
select new
{
CompanyName = companies.CompanyName,
ExecutiveName = exec.ExecutiveName,
//Executive = ce.Executive,
DateAppointment = ce.DateAppointment,
DateResignation = ce.DateResignation
};

// Create a query from the entityset
ObjectQuery companyexecutives = (ObjectQuery)query;

// Return the results
return companyexecutives.Execute(MergeOption.AppendOnly);
}</pre>
My grid has 5 cols and I've set the dataproperty of the each to CompantName,ExecutiveName, DateAppointment, DateResignation but for Executive I want to have a combobox list of Yes/No and if yes is selected I want to update my EDM with 1 else 2 for No.

1 solution

You must create your own class type and override the ToString() method to return the text you want. Here is the sample code:
C#
public class ComboboxItem
{
    public string Text { get; set; }
    public object Value { get; set; }

    public override string ToString()
    {
        return Text;
    }
}


Usage method will be:
C#
private void Test()
{
    ComboboxItem item1 = new ComboboxItem();
    item1.Text = "Yes";
    item1.Value = 1;
    comboBox1.Items.Add(item1);

    ComboboxItem item2 = new ComboboxItem();
    item2.Text = "No";
    item2.Value = 2;
    comboBox1.Items.Add(item2);

    comboBox1.SelectedIndex = 0;
}
 
Share this answer
 
Comments
Uday P.Singh 9-Mar-12 11:40am    
this will do it 5!
ProEnggSoft 9-Mar-12 12:04pm    
Good solution with code. My 5
Shahin Khorshidnia 10-Mar-12 7:17am    
+5

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