0

I bind entity object to gridcontrol

    {
        InitializeComponent();

        gridControl1.DataContext = from q in myEnt.item
                                   from b in myEnt.item_type
                                   where q.item_type_fk == b.item_type_id
                                               select new { q.item_name, q.item,m_type};

    }

which working well. I want to show a related data in a textbox when I click a row in a grid control. How can I do it?I tried this:

<TextBox Name="TextBox3" Text="{Binding Path=item_name}"/>

doesn't work.

2 Answers 2

1

Try this (you need to tell the TextBox exactly where to find item_name):

<TextBox Name="TextBox3" 
         Text="{Binding ElementName=gridControl1, Path=SelectedItem.item_name}"/>

EDIT:

gridControl1 seems to be a DevExpress GridControl which doesn't have a SelectedItem property(?). According to this support article Change selected item through databinding, this binding might work instead (TableView.FocusedRow):

<TextBox Name="TextBox3" 
         Text="{Binding ElementName=tableView1, Path=FocusedRow.item_name}" />
Sign up to request clarification or add additional context in comments.

3 Comments

It does not work. I have to refer to the entity object. How to display a field that is not included in the grid?
What do you mean? item_name is one of the fields in the grid, isn't it? If you don't see anything in the TextBox, you could check the "Output" window in Visual Studio and search for binding errors when you run your project. Also, if you post more of the XAML here, it might be easier to see what's going on.
Of course "gridControl1" has to be the name of the DataGrid. Please post more Code user.
0
<dxr:DXRibbonWindow
x:Class="Eszkoz.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Eszkoz"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
xmlns:dxr="http://schemas.devexpress.com/winfx/2008/xaml/ribbon"
xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
xmlns:dxbh="http://schemas.devexpress.com/winfx/2008/xaml/bars/internal"
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
xmlns:dxd="http://schemas.devexpress.com/winfx/2008/xaml/docking"
Title="DXApplication" Height="700" Width="1100"
SnapsToDevicePixels="True" UseLayoutRounding="True"
dx:ThemeManager.ThemeName="Office2013" xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid">
<dxr:DXRibbonWindow.Resources>
    <dx:EntitySimpleDataSource x:Key="EntitySimpleDataSource" ContextType="local:eszkozEntities" dx:DesignDataManager.DesignData="{dx:DesignDataSettings RowCount=5, UseDistinctValues=True}" Path="eszkoz" />
</dxr:DXRibbonWindow.Resources>

<dxb:BarManager x:Name="barManager" dxbh:BlendHelperForBarsAndRibbon.IsDesignControl="true">
    <DockPanel>
        <dxd:DockLayoutManager x:Name="dockLayoutManager">
            <dxd:LayoutGroup>
                <dxd:LayoutPanel ItemWidth="200" Caption="Navigation" Padding="1">
                    <dxg:GridControl AutoPopulateColumns="True" Name="gridControl1" ItemsSource="{Binding}">
                        <dxg:GridControl.View>
                            <dxg:TableView Name="tableView1" ShowTotalSummary="True" />
                        </dxg:GridControl.View>
                    </dxg:GridControl>
                </dxd:LayoutPanel>
                <dxd:LayoutGroup Orientation="Vertical" ItemWidth="4*">
                    <dxd:LayoutPanel Caption="MainView" ItemHeight="3*">
                        <dxd:LayoutGroup Orientation="Vertical">
                            <dxd:LayoutControlItem Caption="Layout Item">
                                <TextBox Height="23" Name="TextBox3" Width="100" Margin="2" HorizontalAlignment="Left" Text="{Binding ElementName=gridControl1, Path=SelectedItem.item_name}"/>
                            </dxd:LayoutControlItem>
                        </dxd:LayoutGroup>
                    </dxd:LayoutPanel>
                    <dxd:LayoutPanel Caption="DetailView" ItemHeight="2*"></dxd:LayoutPanel>
                </dxd:LayoutGroup>
            </dxd:LayoutGroup>
        </dxd:DockLayoutManager>
    </DockPanel>
</dxb:BarManager>

3 Comments

I haven't used DevExpress' components myself, but I have updated my answer with a suggestion.
I have to refer to the entity object. How to display a field that is not included in the grid?
Then I would add the complete entity objects to the grid, but only make columns for what you want to display in the grid (I assume you can set AutoPopulateColumns=false, and then DevExpress has some way for you to make your own set of columns). That way, you'll have access to the selected entity object through TableView.FocusedRow.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.