Click here to Skip to main content
15,906,097 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I am selecting from listbox it should display in data grid.
It is not getting, please help me.
When I kept dg.itemsource or dg.datacontext it is not coming I have tried.

Here is the code
HTML
<navigation:Page xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  x:Class="NorthSilver.Drop"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
           mc:Ignorable="d"
           xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
           d:DesignWidth="640" d:DesignHeight="480"
           Title="Drop Page" Loaded="Page_Loaded" >
    <navigation:Page.Resources>
        <DataTemplate x:Key="Cattemplate">
            <StackPanel Margin="0,0,0,5" Orientation="Horizontal">
                <TextBlock Margin="10,0,0,0" VerticalAlignment="Center" Text="{Binding Path=CategoryID}"/>
                <TextBlock Margin="10,0,0,0" VerticalAlignment="Center" Text="{Binding Path=CategoryName}"/>
            </StackPanel>
        </DataTemplate>
    </navigation:Page.Resources>
    <Grid x:Name="LayoutRoot">
        <Grid.RowDefinitions>
            <RowDefinition Height="120"></RowDefinition>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="300"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <sdk:DataGrid Name="DG" Margin="12,0,12,0" />
        <ListBox Grid.Row="0" Grid.Column="1" Name="lstcategores" Margin="10,0,10,0" SelectionChanged="lstcategores_SelectionChanged" ItemTemplate="{StaticResource Cattemplate}"/>
    </Grid>
</navigation:Page>


xaml.cs
C#
 private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            prod = new ProductServiceReference.ProductServiceClient();
            prod.GetProductsByCategoryCompleted+= prodservice_GetProductscompleted;
            prod.GetCategoriesCompleted += prodservice_GetCategoriescompleted;
            prod.GetProductsAsync();
            prod.GetCategoriesAsync();
        }

        private void prodservice_GetCategoriescompleted(object sender, ProductServiceReference.GetCategoriesCompletedEventArgs e)
        {
            lstcategores.ItemsSource = e.Result;
        }



        private void lstcategores_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int CategoryID = ((ProductServiceReference.Categories)((ListBox)sender).SelectedItem).CategoryID;
            prod.GetProductsByCategoryAsync(CategoryID);

        }

        private void prodservice_GetProductscompleted(object sender, ProductServiceReference.GetProductsByCategoryCompletedEventArgs e)
        {
            DG.DataContext = e.Result;

        }

in Services
public bool GetProductsByCategory(int categoryid)
        {
            NorthwindClassesDataContext db = new NorthwindClassesDataContext();
            List<Products> prod=new List<Products>();
            try
            {
                prod = (from p in db.Products
                            where p.CategoryID == categoryid
                            select new Products { ProductID = p.ProductID, ProductName = p.ProductName }).ToList();
            }
            catch (Exception)
            {
                throw new FaultException("no Categories are defined");
            }
            return true;
        }
Posted
Updated 6-Oct-11 2:37am
v2

1 solution

i think it is because you have only defined a DataContext for the Datagrid and not bound to anything within the DataContext

try setting the ItemsSource of teh Datagrid
 
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