Home Forums WPF controls Xceed DataGrid for WPF How to bind a DataTable to a ComboBox in WPF DataGrid?

Viewing 15 posts - 1 through 15 (of 18 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #27115 |

    Hi,

    How to bind a DataTable in the same code-behind to the ComboBox column of the DataGrid?

     

    Thanks,

    Venkatesh Mookkan 

    Imported from legacy forums. Posted by Venkatesh (had 9592 views)

    Xceed Support
    Member
    Post count: 5658

    The Solid Foundation sample will provide you with all the code and examples to set up what you are trying to do.

    You can also refer to this thread:

    http://xceed.com/CS/forums/thread/11810.aspx

    Imported from legacy forums. Posted by Marcus [Xceed] (had 2228 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi Marcus,

    I tried the example given over there. But it failed. I have pasted the XAML of my Grid control.

     

                        <xceed:DataGridControl Margin=”10″ Height=”350″ Name=”MappingGrid” Width=”Auto” VerticalAlignment=”Top” CellEditorDisplayConditions=”MouseOverCell” ValidationMode=”CellEndingEdit”>                                                                  
                                <xceed:DataGridControl.View>
                                    <xceed:TableView />                                                                       
                                </xceed:DataGridControl.View>
                                <xceed:DataGridControl.Columns>
                                    <xceed:Column FieldName=”sequence” Title=”Sequence” ReadOnly=”True” Width=”60″ />
                                    <xceed:Column FieldName=”sample_data” Title=”Sample data” ReadOnly=”True” Width=”150″ />
                                    <xceed:Column FieldName=”field_name” Title=”Source Field Name” Width=”150″>
                                        <xceed:Column.CellValidationRules>
                                            <validation:SourceFieldValidationRule />
                                        </xceed:Column.CellValidationRules>
                                    </xceed:Column>
                                    <xceed:Column FieldName=”target_info” Title=”Target Field” CellContentTemplate=”{StaticResource MappingStyle}” CellEditor=”{StaticResource MappingEditStyle}” Width=”200″ />
                                    <xceed:Column FieldName=”target_db” Title=”Target DB” Visible=”False” />
                                    <xceed:Column FieldName=”target_table” Title=”Target Table” Visible=”False” />
                                    <xceed:Column FieldName=”target_column” Title=”Target Field” Visible=”False” />
                                </xceed:DataGridControl.Columns>
                            </xceed:DataGridControl>

     

    I have a DataTable in the same code behind say “SourceFieldsTable”. I want to bind that to the Column with FieldName = “field_name”. I created a CellEditor with a ComboBox and Binded the DataTable. Its showing empty ComboBox only.

    Imported from legacy forums. Posted by Venkatesh (had 6127 views)

    Xceed Support
    Member
    Post count: 5658

    Do you have any binding errors in the output window?

    Imported from legacy forums. Posted by Jenny [Xceed] (had 1947 views)

    User (Old forums)
    Member
    Post count: 23064

    No Jenny.

    Imported from legacy forums. Posted by Venkatesh (had 1091 views)

    Xceed Support
    Member
    Post count: 5658

    Can you provide more details, such as the XAML for the MappingEditStyle?

    Imported from legacy forums. Posted by Jenny [Xceed] (had 1040 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi Jenny,

    See the below which is the CellEditor I used before for the ComboBox which is not working.

    <xceed:CellEditor x:Key=”SourceFieldEditStyle”>
                <xceed:CellEditor.EditTemplate>
                    <DataTemplate>
                        <StackPanel Orientation=”Horizontal” HorizontalAlignment=”Stretch” AllowDrop=”True”>
                            <ComboBox ItemsSource=”{Binding dataMapFields}” DisplayMemberPath=”field_name” SelectedValuePath=”field_name” HorizontalAlignment=”Stretch”/>
                    </StackPanel>
                    </DataTemplate>
                </xceed:CellEditor.EditTemplate>
            </xceed:CellEditor>

     

    This CellEditor is used for the Column with FieldName “field_name”. Here “dataMapFields” is the DataTable in the codebehind. I hope this details would help to debug my problem.

     
    If possible give me a sample code that will load DataTable to a ComboBox in a DataGridControl. The filling up of the DataTable should be dynamic and code should not be in App Context.

     

    Thanks
     

    Imported from legacy forums. Posted by Venkatesh (had 6200 views)

    Xceed Support
    Member
    Post count: 5658

    You also need to bind the SelectedValue of the ComboBox using a CellEditorBinding. You can refer to this post for an example.

    Imported from legacy forums. Posted by Jenny [Xceed] (had 1354 views)

    User (Old forums)
    Member
    Post count: 23064

    Hey Jenny,

    I tried with the post you mentioned. But it is not working. The ComboBox is show empty still. I changed my CellEditor resource as,

     
            <xceed:CellEditor x:Key=”SourceFieldEditStyle”>
                <xceed:CellEditor.EditTemplate>
                    <DataTemplate>
                        <StackPanel Orientation=”Horizontal” HorizontalAlignment=”Stretch” AllowDrop=”True”>
                            <ComboBox ItemsSource=”{Binding dataMapFields}” SelectedValue=”{xceed:CellEditorBinding}”  DisplayMemberPath=”field_name” SelectedValuePath=”field_name” HorizontalAlignment=”Stretch”/>
                    </StackPanel>
                    </DataTemplate>
                </xceed:CellEditor.EditTemplate>
            </xceed:CellEditor>

    Thanks

    Imported from legacy forums. Posted by Venkatesh (had 4681 views)

    User (Old forums)
    Member
    Post count: 23064

    Does this problem resolvable?

    Imported from legacy forums. Posted by Venkatesh (had 854 views)

    Xceed Support
    Member
    Post count: 5658

    Hi Venkatesh,

       I’ve looked through your code and I’ve been wondering: Is there a “dataMapFields” property on the objects mapped to the cell for the Column?

       I do ask because I notice that the binding to load the ComboBox ItemsSource refers to a Binding to a “dataMapFields” on the DataContext.

       <ComboBox ItemsSource=”{Binding dataMapFields}”

       If there is no “dataMapFields” property on the Cell`s Content, then it is normal that you are not seeing anything in the combo box.

       You will need to assess the appropriate location for the “dataMapFields” and create a binding in consequence (or use the Loaded event of the ComboBox to manually load the ItemsSource in code-behind.

    Imported from legacy forums. Posted by Marcus [Xceed] (had 1239 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi Marcus,

    I am bit confused here.

        I do ask because I notice that the binding to load the ComboBox
    ItemsSource refers to a Binding to a “dataMapFields” on the DataContext.

       <ComboBox ItemsSource=”{Binding dataMapFields}”

      
    If there is no “dataMapFields” property on the Cell`s Content, then it
    is normal that you are not seeing anything in the combo box.

     “dataMapFields” is a DataTable and how could I assign that to the Cell’s Content. Leave my code. Could you please give a sample code that a ComboBox Cell which loads from a DataTable where the DataTable is in the code behind. I am trying to get the answer for this question from very beginning. But everybody is pointing to an existing example which come with DataGridControl. That example is using DataTable from App context. I hope I explained my issue clearly.

     

    Imported from legacy forums. Posted by Venkatesh (had 1425 views)

    Xceed Support
    Member
    Post count: 5658

    [quote user=”Venkatesh”]

    Could you please give a sample code that a ComboBox Cell which loads from a DataTable where the DataTable is in the code behind. I am trying to get the answer for this question from very beginning. But everybody is pointing to an existing example which come with DataGridControl. That example is using DataTable from App context.

    [/quote]

    Application.Current is just the location I chose to place the property that exposes my data source. That said, you can have your property anywhere as long as the grid has access to it. For example, if you have a Window1 that contains  your grid, you could do an AncestorType binding to find the window and get the property.

    {Binding RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}, Path=dataMapFields}

    This binding assumes that you have declared an xmlns named local for your local namespace.

     

    Imported from legacy forums. Posted by Jenny [Xceed] (had 1131 views)

    User (Old forums)
    Member
    Post count: 23064

    Jenny,

    I feel bad asking you again and again with the same problem. I had changed the binding as you mentioned by no use.

     <xceed:CellEditor x:Key=”SourceFieldEditStyle”>
                <xceed:CellEditor.EditTemplate>
                    <DataTemplate>
                        <StackPanel Orientation=”Horizontal” HorizontalAlignment=”Stretch” AllowDrop=”True”>
                            <ComboBox ItemsSource=”{Binding RelativeSource={RelativeSource AncestorType={x:Type local:DataMapWindow}}, Path=dataMapFields}” SelectedValue=”{xceed:CellEditorBinding}”  DisplayMemberPath=”field_name” SelectedValuePath=”field_name” HorizontalAlignment=”Stretch”/>
                    </StackPanel>
                    </DataTemplate>
                </xceed:CellEditor.EditTemplate>
            </xceed:CellEditor>

     
    Is there anything, that I could send you, so that you can find the problem easily?
     

    Imported from legacy forums. Posted by Venkatesh (had 1782 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi Jenny,

    Let’s see you have a WPF project with

    • Window1 form in namespace NS
    • In the Window1 codebehind, you have a dynamically filled DataTable named DT with 2 columns, ID and NAME
    • In Window1 you have a Grid named GRID
    • In GRID you have a column called COL with type of int refers to the DT/ID column
    • You want to display a combobox in GRID/COL showing DT/NAME with DT/ID as value

    Please write all the necessary codes in XAML (especially the Bindings everywhere!) and in C# that is needed to solve this problem. I’d like to ask you not to suggest to me any sample codes because in those there are pre-filled into-dll-compiled datasets what are so far away from reality.

    Thanks in advance, hope you can help because it’s been very long to solve this problem,
    Peter 

    Imported from legacy forums. Posted by Hardware (had 1841 views)

Viewing 15 posts - 1 through 15 (of 18 total)
  • You must be logged in to reply to this topic.