sreekanth v

sreekanth v

  • NA
  • 51
  • 0

Listbox and datagrid

Oct 6 2011 8:32 AM
when i am selecting from listbox it should display in datagrid. it is not getting plz help me.
when i kept dg.itemsource or dg.datacontext it is not coming i have tried
here the code.
<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
 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;
        }

Answers (1)