Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how do I convert string values to integers and back using IValueConverter.
I have a database that consist of two tables, table CompanyX and table DeptY.
Table CompanyX has field ID, firstName, lastName, Email, Phone.
Table DeptY has field dID, Roles.
DeptY dID is foreign key To CompanyX ID.

//To cut the story short this is my ItemTemplate below:
<application.resources>
        <datatemplate x:key="myTemplate" xmlns:x="#unknown">
            <wrappanel horizontalalignment="Stretch">
                <textblock text="{Binding FirstName}" />
                <Label />
                <textblock text="{Binding LastName}" />
            </wrappanel>
        </datatemplate>
</application.resources>

//My ComboBox:
<combobox height="23" horizontalalignment="Right" margin="0,90,267,0" name="cboID" itemssource="{Binding}" displaymemberpath="" verticalalignment="Top" width="208" itemtemplate="{StaticResource myTemplate}" />

//My DataGrid:
<datagridtemplatecolumn.celltemplate>
                                    <datatemplate>
  <textblock text="{Binding Path=TID, Converter={StaticResource myConverter}}" />    
                                    </datatemplate>
                                </datagridtemplatecolumn.celltemplate> 

//And my IValueConverter:
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
       {
           string a = (string)value;
           int b;
           int.TryParse(a, out b);
           return b;
       }
       public object ConvertBack(object value, Type targetTypes, object parameter, System.Globalization.CultureInfo culture)
       {
           throw new NotImplementedException();
       }

Can someone help please
Posted
Updated 9-May-14 2:26am
v2
Comments
[no name] 9-May-14 8:25am    
Help with what? What is wrong with the code that you have?
Member 10703465 9-May-14 9:42am    
I Want to display in a datagrid the ID of the person i'm selecting from the ComboBox
[no name] 9-May-14 11:07am    
Okay... and? Telling us "I want" is not a question or a description of a problem. You need to tell us what the problem with your code is.

1 solution

How do I convert string values to integers and back using IValueConverter.
I have a database that consist of two tables, table CompanyX and table DeptY.
Table CompanyX has field ID(int), firstName, lastName, Email, Phone.
Table DeptY has field pID(int), Roles.
DeptY pID is foreign key To CompanyX ID.
Every time I select someone in the Combobox, I want it to displayed as ID in a DataGrid.

//To cut the story short this is my ItemTemplate below:
<Application.Resources>
<DataTemplate x:Key="myTemplate">
<WrapPanel HorizontalAlignment="Stretch">
<TextBlock Text="{Binding FirstName}"/>
<Label />
<TextBlock Text="{Binding LastName}"/>
</WrapPanel>
</DataTemplate>
</Application.Resources>

This is my Combobox which is bound to the ItemTemplate:

<ComboBox Height="23" HorizontalAlignment="Right" Margin="0,90,267,0" Name="comboID" ItemsSource="{Binding}" VerticalAlignment="Top" Width="208" ItemTemplate="{StaticResource myTemplate}" />

And a DataGrid which displays:

<DataGridTemplateColumn x:Name="pIDColumn" Header="Person ID" Width="auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=pID, Converter={StaticResource myConverter}}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn x:Name="rolesColumn" Header="Roles" Width="auto" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Roles}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

IValueConverter which is not converting!!

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string a = (string)value;
int b;
int.TryParse(a, out b);
return b;
}
public object ConvertBack(object value, Type targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}

can someone please help
 
Share this answer
 

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