Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi guys,

I can't figured it out... For example I have DataTable "Table1" with "ID" and "Names" columns.
Rows look like:
ID Names
1 test1|test2|test3
2 test4|test5|test6

I bind ID column like this:


C#
C#
...
DataSet dataset = new DataSet();
DataGridView.ItemSource = dataset.Table1.DefaultView;
...

XAML
...
<DataGrid.Columns>
 <DataGridTextColumn Binding="{Binding Path=ID}" Header="ID" />
...


This part works well. But how can I bind combobox column for "Names" in my situation?

Please give me direction. Thank you.

[UPDATE]
"Names" has values like "test1|test2|test3" in DataSet for each row.
I'm looking for solution to display value "test1|test2|test3" from DataSet in DataGridView row with ComboBox which will have items:
- test1
- test2
- test3

Let me know if it's still unclear

Timur.
Posted
Updated 15-Feb-12 23:27pm
v2
Comments
Mohamed Ahmed Abdullah 13-Feb-12 14:45pm    
try this
<combobox itemssource="{Binding Names}"/>
Nestor Arturo 15-Feb-12 8:26am    
Do you mean "Names" has a value like "test1|test2|test3", all in the same column?
TimGameDev 16-Feb-12 5:30am    
I've updated my question with details. Let me know if it's still unclear

In your ViewModel define a property like:

public IEnumerable<string> Names
{
get
{
   return nameColumn.Split('|');
}
}


Then bind your ComboBox ItemsSource to Names

Ok, the ViewModel will look like:

public class MainViewModel
{
    private string contentNames = "test1|test2|test3";

    public IEnumerable<string> SplitContentNames
    {
        get
        {
            return contentNames.Split('|');
        }
    }
}


The XAML will look like:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <ComboBox ItemsSource="{Binding SplitContentNames}"/>
</Grid>


And the code behind of MainViewWindow.cs will bind the DtaContext like so:
this.DataContext = new MainViewModel();


Then to get the selected item into your view model, add a property for your selected item:

public string SelectedName
{
    get;
    set;
}


and then bind the SelectedValue using XAML:

<ComboBox ItemsSource="{Binding SplitContentNames}" SelectedValue="{Binding SelectedName}"/>


Now when you change the value in the combobox the current value will be set in the SelectedName property.
 
Share this answer
 
v4
Comments
TimGameDev 19-Feb-12 10:20am    
Unfortunately I cannot make your solution works. Anyway I found worked solution by myself. It's binding converter.
SteveAdey 19-Feb-12 11:05am    
hmmm, not sure why you couldn't get this to work, given your solution, as they should equate to the same thing. Mine's just doing it in the ViewModel, while yours is doing it in a converter (IMHO not the way to go). But, if it works for you...
TimGameDev 19-Feb-12 12:07pm    
Steve, could you please provide detailed example how I should change ViewModel and bind combobox to new property? Probably I do something wrong.
And one more question - what is problem with converters from your point?

Thanks for participation.
SteveAdey 19-Feb-12 16:31pm    
Hi Timur, I've now updated my solution with an example. My main reason for not recommending the Value Converter in this case, is that it's something the view model should be dealing with. The Converters are really to convert data from one type into a more visual type. This will do what you want, but what if some data came from another source as was not delimited by the '|' character? Or you had a list that was not delimited at all? This would make for some very messy XAML. I'm not saying that you shouldn't use it here, just that it's not good practice.
TimGameDev 20-Feb-12 18:22pm    
Thank you very match! My problem was in Binding, incorrect source was specified...Now it works perfect. If you don't mind I leave my solution as accepted also (as alternative)

Timur.
Okay, any solutions represented here do not work for me.

I solved by myself again. Here is the solution:

Create class like:
C#
[ValueConversion(typeof(String), typeof(String))]
public class StringSplitterConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value.ToString().Split('|');
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value;
    }
}


Use this converter in combobox binding:
C#
...
            <datagrid.resources>
                <local:stringsplitterconverter x:key="splitterConverter" xmlns:x="#unknown" xmlns:local="#unknown" />
            </datagrid.resources>
...
                    <datagridtemplatecolumn.celltemplate>
                        <datatemplate>
                            <combobox itemssource="{Binding Path=ContentNames, Converter={StaticResource splitterConverter}}"></combobox>
                        </datatemplate>
                    </datagridtemplatecolumn.celltemplate>


This solution works great.
 
Share this answer
 
Try this
HTML
<datagridcomboboxcolumn header="Column Header" selecteditembinding="{Binding ID} DisplayMemberPath=" hold=" /></pre><br mode="></datagridcomboboxcolumn>
 
Share this answer
 
Comments
TimGameDev 16-Feb-12 5:31am    
It will display ComboBox with one item which will have value "test1|test2|test3", isn't it? Please check my question again.

Thanks, Timur

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