0

I am Using ListBox Control to Show Items And When I double Cilck The Item means Thier particular item fields will Display in xamgrid and the problem is when i Selected item means its showing but I need to display multiple things in grid for one or more items

my Codings Are,

private void LsImageGallery_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            RTBSPROJECT.DB.RTBSInventoryMgmtEntities2 MyRTBSinventoryDB = new DB.RTBSInventoryMgmtEntities2();

            RTBSPROJECT.DB.RTBSInventoryMgmtEntities2 DB_Linq = new DB.RTBSInventoryMgmtEntities2();
         try
        {

        string curItem = LsImageGallery.SelectedItem.ToString();

        #region

        if (cmbItemCombo.SelectedItem == null)
        {


            var SelectedImage = (ImageEntity)this.LsImageGallery.SelectedItem;

            string ItemName = SelectedImage.ItemName_EN;

            var query = (from MyItems in MyRTBSinventoryDB.tbl_ItemMaster
                         where MyItems.ActiveFlag == true &&
                         MyItems.ItemName_EN == ItemName
                         select MyItems).ToList();

            xamGrid1.ItemsSource = query;
}

Every Time Its Displaying one record ,If i selected second one means previous selected should not clear both should display please help me..

1
  • Please Some One reply Commented Jul 17, 2013 at 13:36

1 Answer 1

0

I'm not 100% sure what you're asking here but I'll have a go anyway. It looks to me like you're trying to add more rows to the DataGrid but instead only the new items are being displayed.

This is because you're changing the ItemsSource property of the DataGrid, if you want your new items to be added to the bottom then the ItemsSource needs to stay the same and you add the new rows.

This can either be done with the Items.Add() method on the DataGrid, loop over your data rows and call add for each one or you could create a collection of Items that you just add to on double click, I've provided an example of the second here.

like your application it shows a ListBox and a DataGrid, each time you double click on an item in the ListBox a few more rows get added to the DataGrid.

XAML

 <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            DataContext="{Binding RelativeSource={RelativeSource Self}}"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <ListBox Grid.Column="0" ItemsSource="{Binding Items}" MouseDoubleClick="ListBox_MouseDoubleClick"/>
            <DataGrid Grid.Column="1" ItemsSource="{Binding GridItems}">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding}"/>
                </DataGrid.Columns>
            </DataGrid>
        </Grid>
    </Window>

Code behind

using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        // This collection is bound to the ListBox so there are items to select from.
        public ObservableCollection<object> Items
        {
            get { return (ObservableCollection<object>)GetValue(ItemsProperty); }
            set { SetValue(ItemsProperty, value); }
        }
        public static readonly DependencyProperty ItemsProperty =
            DependencyProperty.Register("Items", typeof(ObservableCollection<object>), typeof(MainWindow), new PropertyMetadata(null));

        // On double click of each item in the ListBox more items will be added to this collection.
        public ObservableCollection<object> GridItems
        {
            get { return (ObservableCollection<object>)GetValue(GridItemsProperty); }
            set { SetValue(GridItemsProperty, value); }
        }       
        public static readonly DependencyProperty GridItemsProperty =
            DependencyProperty.Register("GridItems", typeof(ObservableCollection<object>), typeof(MainWindow), new PropertyMetadata(null));

        public MainWindow()
        {
            InitializeComponent();

            // Some demo items so we can double click.
            Items = new ObservableCollection<object>();
            Items.Add("test item 1");
            Items.Add("test item 2");
            Items.Add("test item 3");
            Items.Add("test item 4");
            Items.Add("test item 5");
            Items.Add("test item 6");
            Items.Add("test item 7");

            GridItems = new ObservableCollection<object>();
        }

        private void ListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            // These would be the entries from your database, I'm going to add random numbers.
            Random rnd = new Random();

            for (int i = 0; i < rnd.Next(5); i++)
                GridItems.Add(rnd.Next(100));
        }
    }
}

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.