BindingList Stream Writing Problem

Oct 7 2011 5:56 AM
Hello,
I have been writing a simple stock take program that uses a BindingList and business object with a DataGridView. However, I have ran into a peculiar problem when attempting to write to a file. The file is empty when exporting. I have explained the issue in more detail below. please, read on.

My Business object called 'ProdItem' (see below) has a bool property called "toExport"(see below) which is represented in the dataGridView as a CheckBox column.

class ProdItem : INotifyPropertyChanged
    {
        private string productCode;
        private Int32 stockStatus;
        private Int32 stockOnHand;
        private bool toExport;

        public event PropertyChangedEventHandler PropertyChanged;

        public ProdItem() { }

        public ProdItem(string productCode,  int stockStatus, int stockOnHand, bool toExport)
        {
            this.productCode = productCode;
            this.stockStatus = stockStatus;
       this.stockOnHand = stockOnHand;
            this.toExport = toExport;
        }

        public string ProductCode
        {
            get { return this.productCode; }
            set
            {
                this.productCode = value;
                this.NotifyPropertyChanged("ProductCode");
            }
        }

        public int StockStatus
        {
            get { return this.stockStatus; }
            set
            {
                this.stockStatus = value;
                this.NotifyPropertyChanged("StockStatus");
            }
        }

        public int StockOnHand
        {
            get { return this.stockOnHand; }
            set
            {
                this.stockOnHand = value;
                this.NotifyPropertyChanged("StockOnHand");
            }
        }

        public bool ToExport
        {
            get { return this.toExport; }
            set
            {
                this.toExport = value;
                this.NotifyPropertyChanged("ToExport");
            }
        }

        private void NotifyPropertyChanged(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }

When a ProdItem is instantiated the constructor by default gives the toExport property a 'False' value (see below).  These are all added to a BindingList which are then bound to a DataGridView. The toExport column is a checkBox column that are all unchecked.

prodList.Add(new ProdItem(str[0], stockStatus, false));

After checking some of the 'To Export' columns check boxes and clicking an export button the following code is used to export the selected objects to a csv file.

{
            Stream myStream;
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();

            saveFileDialog1.Filter = "Comma Separated Values (*.csv)|*.csv|All Files {*.*}|*.*";
            saveFileDialog1.RestoreDirectory = true;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = saveFileDialog1.OpenFile()) != null)
                {
                    StreamWriter sw = new StreamWriter(myStream);

                    foreach (ProdItem i in prodList)
                    {
                        if (i.ToExport != false)
                        {
                            sw.WriteLine(i.ProductCode + "," + i.StockOnHand);
                        }
                    }
                    myStream.Close();
                }
            }
 }

Now, this is where it gets weird. For some reason this does not work, It will always create an empty file. However, If for instance I change the If loop on 'i.ToExport' to a 'equal to operator' instead of a 'not equal to operator' it will correctly write all the objects that are not checked in the 'To Export' column just fine. WHY!!!! I have tried all the possible combinations such as 'equal to' true instead of 'not equal to' false and the same thing happens. I have also made sure that the if loop is catching the 'ToExport' checked objects using break points. Everything looks to be working fine right up to the last bit where the file is aggravatingly empty.

I have also noticed that for some reason it also doesn't work when instantiating all my business objects with a false bool value, however it works ok when instantiated with a true bool value(this makes all the check boxes checked). Huh!?!?

Please help. Thanks.

-KC

Answers (3)