none
adding items to a list in c# wpf using form controls RRS feed

  • Question

  • I have the below code that takes textbox data and addes an item to a list but after outputting the list to a data grid it appears that every time I add an item to the list the list gets cleared as I only get the latest item data on the data grid. What am I doing wrong?

    namespace Training2.Windows
    {
        /// <summary>
        /// Interaction logic for LotScan.xaml
        /// </summary>
        public partial class LotScan : Window
        {
            public LotScan()
            {
                InitializeComponent();
        
            }

            public class LotData
            {
                public string Lot;
                public string Description { get; set; }
                public int PO { get; set; }
                public string MfgPart { get; set; }
            }

            private List<LotData> LoadCollectionData()
            {
                List<LotData> lot = new List<LotData>();
                lot.Add(new LotData()
                {
                    Lot = LotNo.Text,
                    Description = frmDescription.Text,
                    PO = int.Parse(frmPO.Text),
                    MfgPart = frmMfgPart.Text,
                });
                return lot;

            }

            private void Button_Click(object sender, RoutedEventArgs e)
            {          
                gridLotData.ItemsSource = LoadCollectionData();

                LotNo.Text = String.Empty;
                frmMfgPart.Text = string.Empty;
                frmDescription.Text = String.Empty;
                frmMfgPart.Text = string.Empty;
                frmPO.Text = string.Empty;
            }


    MeToo_

    Thursday, October 10, 2019 8:51 PM

Answers

  • Hi,
    you must instantiate the List only once. It's better to use ObservableCollection instead of List.

    using System.Collections.ObjectModel; ... private ObservableCollection<LotData> lot = new ObservableCollection<LotData>();

    private ObservableCollection<LotData> LoadCollectionData() { // List<LotData> lot = new List<LotData>(); lot.Add(new LotData() { Lot = LotNo.Text, Description = frmDescription.Text, PO = int.Parse(frmPO.Text), MfgPart = frmMfgPart.Text, }); return lot; }



    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    • Marked as answer by MeToo_ Friday, October 11, 2019 1:08 PM
    Friday, October 11, 2019 3:55 AM

All replies

  • Hi MeToo,

    Thank you for posting here.

    Since this thread is related to WPF, I will move it to WPF forum to get professional support.

    The Visual C# forum discusses and asks questions about the C# programming language, IDE, libraries, samples, and tools.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 11, 2019 1:39 AM
  • Hi,
    you must instantiate the List only once. It's better to use ObservableCollection instead of List.

    using System.Collections.ObjectModel; ... private ObservableCollection<LotData> lot = new ObservableCollection<LotData>();

    private ObservableCollection<LotData> LoadCollectionData() { // List<LotData> lot = new List<LotData>(); lot.Add(new LotData() { Lot = LotNo.Text, Description = frmDescription.Text, PO = int.Parse(frmPO.Text), MfgPart = frmMfgPart.Text, }); return lot; }



    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    • Marked as answer by MeToo_ Friday, October 11, 2019 1:08 PM
    Friday, October 11, 2019 3:55 AM