Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Общие вопросы по .NET и C# > DataGrid.ItemsSource


Автор: strS 27.8.2011, 20:43
Добрый вечер.


Код

        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            DBWork.menuDataSet menuDataSet = ((DBWork.menuDataSet)(this.FindResource("menuDataSet")));
            // Load data into the table images. You can modify this code as needed.
            DBWork.menuDataSetTableAdapters.imagesTableAdapter menuDataSetimagesTableAdapter = new DBWork.menuDataSetTableAdapters.imagesTableAdapter();
            menuDataSetimagesTableAdapter.Fill(menuDataSet.images);
            System.Windows.Data.CollectionViewSource imagesViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("imagesViewSource")));
            imagesViewSource.View.MoveCurrentToFirst();
            // Load data into the table images. You can modify this code as needed.
            DBWork.menuDataSetTableAdapters.firstTableAdapter menuDataSetfirstTableAdapter = new DBWork.menuDataSetTableAdapters.firstTableAdapter();
            menuDataSetfirstTableAdapter.Fill(menuDataSet.first);
            System.Windows.Data.CollectionViewSource firstViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("firstViewSource")));
            firstViewSource.View.MoveCurrentToFirst();
        }


Код

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            imagesDataGrid.ItemsSource = "{Binding Source={StaticResource imagesViewSource}}"; //тут должно быть что-то другое, но что((
        }



Код

<Window x:Class="DBWork.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" xmlns:my="clr-namespace:DBWork" Loaded="Window_Loaded_1">
    <Window.Resources>
        <my:menuDataSet x:Key="menuDataSet" />
        <CollectionViewSource x:Key="imagesViewSource" Source="{Binding Path=images, Source={StaticResource menuDataSet}}" />
        <CollectionViewSource x:Key="firstViewSource" Source="{Binding Path=first, Source={StaticResource menuDataSet}}" />
    </Window.Resources>
    <Grid Height="321" Name="grid1" Width="515" >
            
        <Button Content="Button" Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="10,28,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />

        <DataGrid AutoGenerateColumns="True" EnableRowVirtualization="True" Height="200" HorizontalAlignment="Left" ItemsSource="{Binding Source={StaticResource firstViewSource}}" Margin="3,-11,0,0" Name="firstDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" VerticalAlignment="Top" Width="400">
        </DataGrid>
        <DataGrid AutoGenerateColumns="True" EnableRowVirtualization="True" Grid.RowSpan="2" Height="200" HorizontalAlignment="Left"  Margin="4,117,0,0" Name="imagesDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" VerticalAlignment="Top" Width="400">
        </DataGrid>
    </Grid>
    
</Window>




В общем первый грид заполняется нормально, а для второго никак не получается правильно задать ItemsSource. Хотелось бы это сделать не через XAML, а в коде.

Автор: strS 28.8.2011, 12:45
все, разобрался... Оказалось надо 
Код

menuDataSet dataset = new menuDataSet();
menuDataSetTableAdapters.imagesTableAdapter adapter2 = new menuDataSetTableAdapters.imagesTableAdapter();
adapter2.Fill(dataset.images);
DataGrid.DataContext = dataset.images.DefaultView;
       
    

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)