将SQL查询结果传递给多个文本框

时间:2012-02-24 21:16:02

标签: c# sql wpf

我有一个由SQL服务器填充的数据网格。我试图从条目列表中选择一行,并将它们输入到网格外的文本框中。我似乎无法找到将查询结果转换为可管理数据的方法,而不是填充网格。

C#

MoviesEntities dataEntities = new MoviesEntities();
ObjectQuery<MovieTable> _movies = dataEntities.MovieTables;

var query =
    from MovieTable in _movies
    where MovieTable.Title == MMEM.MovieTitle
    select MovieTable.id;
    string result = Convert.ToString(query); //or something??

XAML:

<Window x:Class="MovieApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" 
    Height="650" 
    Width="525"
    ResizeMode="NoResize"
    WindowStartupLocation="CenterScreen"
    xmlns:vm="clr-namespace:MovieApp">
<Window.Resources>
    <vm:MovieAppViewModel x:Key="MovieAppViewModel"/>
</Window.Resources>
<StackPanel x:Name="RootStackPanel" DataContext="{StaticResource MovieAppViewModel}">
    <Grid HorizontalAlignment="Center" Height="99" Width="440">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <TextBlock Grid.Column="0" Text="Movie Title" Margin="30 0 30 0" HorizontalAlignment="Center" FontWeight="Heavy"/>
        <TextBlock Grid.Column="1" Text="Rating" Margin="30 0 30 0"  HorizontalAlignment="Center" FontWeight="Heavy"/>
        <TextBlock Grid.Column="2" Text="Stars" Margin="30 0 30 0" HorizontalAlignment="Center" FontWeight="Heavy"/>
        <TextBlock Grid.Column="3" Text="Release Year"  Margin="30 0 30 0" HorizontalAlignment="Center" FontWeight="Heavy"/>
        <TextBox Grid.Column="0" TextAlignment="Center" Text="{Binding MMEM.MovieTitle}"  IsEnabled="{Binding MMEM.IsEditable}" Grid.Row="1" MaxLength="50" Width="100"/>
        <TextBox Grid.Column="1" TextAlignment="Center" Text="{Binding MMEM.Rating}"  IsEnabled="{Binding MMEM.IsEditable}" Grid.Row="1" MaxLength="5" Width="55"/>
        <TextBox Grid.Column="2" TextAlignment="Center" Text="{Binding MMEM.Stars}"  IsEnabled="{Binding MMEM.IsEditable}" Grid.Row="1" MaxLength="5" Width="20"/>
        <TextBox Grid.Column="3" TextAlignment="Center" Text="{Binding MMEM.ReleaseYear}"  IsEnabled="{Binding MMEM.IsEditable}" Grid.Row="1" MaxLength="20" Width="100"/>
    </Grid>
    <Button Content="Edit Mode" Command="{Binding ToggleCommand}"/>
    <DataGrid 
        Width="Auto" 
        SelectionMode="Extended" 
        IsReadOnly="True" 
        AutoGenerateColumns="False" 
        ItemsSource="{Binding MMLM.MoviesList}"
        DataContext="{StaticResource MovieAppViewModel}">
        <DataGrid.Columns>
            <DataGridTextColumn Width="200" Header="Title" Binding="{Binding Title}"/>
            <DataGridTextColumn Width="100" Header="Rating" Binding="{Binding Rating}"/>
            <DataGridTextColumn Width="100" Header="Stars" Binding="{Binding Stars}"/>
            <DataGridTextColumn Width="93" Header="Release Year" Binding="{Binding ReleaseYear}"/>
        </DataGrid.Columns>
    </DataGrid>
</StackPanel>

我假设有一种比将行解析为字符串并填充文本框更有效的方法,但此时我将采取我能得到的内容!

感谢您的任何输入!!!!

1 个答案:

答案 0 :(得分:1)

只需命名数据网格,例如masterDG

然后在细节

 Binding ElementName=masterDG, path=SelecedItem.Title