每次更改ComboBox 1时,ComboBox 2中的日期总是会更改

时间:2013-09-22 07:24:43

标签: c# winforms combobox

我的程序中有2个ComboBox(ComboBox 1和ComboBox 2),我遇到了问题,当我在ComboBox 1中选择日期“10/10/2014”时,ComboBox 2的改变与我的完全相同在ComboBox 1中,为什么会这样?

以下是代码:

string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\db1.accdb";

        private const int CP_NOCLOSE_BUTTON = 0x200;

        private Choices _choice;

        private DataSet _ds = new DataSet();

        private List<DateTime> _dateTime = new List<DateTime>();

        public Trans()
        {
            InitializeComponent();
        }

        public Trans(Choices _choice)
            : this()
        {
            this._choice = _choice;
        }

        private void Trans_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < DateTime.Today.AddYears(1).Subtract(DateTime.Today).TotalDays + 1; i++)
            {
                _dateTime.Add(DateTime.Today.AddDays(i));
            }

            ViewDatabase(sender, e);
            StartDateCollection(sender, e);
            EndDateCollection(sender, e);

            this.dataGridView1.Columns["ID"].Visible = false;
            this.dataGridView1.Sort(this.dataGridView1.Columns["Times"], System.ComponentModel.ListSortDirection.Ascending);
            this.label3.Text = "Welcome, " + UserInformation.CurrentLoggedInUser + " " + " " + "-" + " " + " " + UserInformation.CurrentLoggedInUserType;
            this.label3.ForeColor = System.Drawing.Color.White;

            dataGridView1.RowPostPaint += new DataGridViewRowPostPaintEventHandler(this.SetRowNumber);
            dataGridView1.ClearSelection();
        }

        private void ViewDatabase(object sender, EventArgs e)
        {
            using (OleDbConnection conn = new OleDbConnection(connectionString))
            {
                string query = "SELECT [ProductCode], [Quantity], [Description], [SubTotal], [Total], [IssuedBy], [To], [Dates], [Times] FROM [TransRecord]";

                conn.Open();

                using (OleDbDataAdapter _adapter = new OleDbDataAdapter(query, conn))
                {
                    _ds.Clear();
                    _adapter.Fill(_ds, "TransRecord");
                    dataGridView1.DataSource = null;
                    dataGridView1.Refresh();
                }

                dataGridView1.DataSource = _ds.Tables[0];

                conn.Close();
            }
        }

        private void SetRowNumber(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            var grid = sender as DataGridView;
            var rowIdx = (e.RowIndex + 1).ToString();

            var centerFormat = new StringFormat()
            {
                Alignment = StringAlignment.Center,
                LineAlignment = StringAlignment.Center
            };

            var headerBounds = new Rectangle(e.RowBounds.Left, e.RowBounds.Top, grid.RowHeadersWidth, e.RowBounds.Height);
            e.Graphics.DrawString(rowIdx, this.Font, SystemBrushes.ControlText, headerBounds, centerFormat);
        }

        private void StartDateCollection(object sender, EventArgs e)
        {
            using (OleDbConnection conn = new OleDbConnection(connectionString))
            {
                string query = "SELECT [Dates] FROM [TransRecord]";

                conn.Open();

                using (OleDbDataAdapter _adapter = new OleDbDataAdapter(query, conn))
                {
                    comboBox1.DataSource = _dateTime;
                    comboBox1.FormatString = "M/dd/yyyy";
                    comboBox1.FormattingEnabled = true;
                }
            }
        }

        private void EndDateCollection(object sender, EventArgs e)
        {
            using (OleDbConnection conn = new OleDbConnection(connectionString))
            {
                string query = "SELECT [Dates] FROM [TransRecord]";

                conn.Open();

                using (OleDbDataAdapter _adapter = new OleDbDataAdapter(query, conn))
                {
                    comboBox2.DataSource = _dateTime;
                    comboBox2.FormatString = "M/dd/yyyy";
                    comboBox2.FormattingEnabled = true;
                }
            }
        }

        private void quitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            QuitProgram(sender, e);
        }

        private void printToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PrintFile(sender, e);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            GetData(sender, e);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Clear(sender, e);
        }

        private void PrintFile(object sender, EventArgs e)
        {

        }

        private void GetData(object sender, EventArgs e)
        {
            using (OleDbConnection conn = new OleDbConnection(connectionString))
            {
                string query = "SELECT [ProductCode], [Quantity], [Description], [SubTotal], [Total], [IssuedBy], [To], [Dates], [Times] FROM [TransRecord] WHERE [Dates] = @Dates ORDER BY [Dates]";

                conn.Open();

                using (OleDbCommand cmd = new OleDbCommand(query, conn))
                {
                    cmd.Parameters.Add("@Dates", System.Data.OleDb.OleDbType.Date);
                    cmd.Parameters["@Dates"].Value = this.comboBox1.SelectedValue;

                    using (OleDbDataAdapter _adapter = new OleDbDataAdapter(cmd))
                    {
                        _ds.Clear();
                        _adapter.Fill(_ds, "TransRecord");
                        dataGridView1.DataSource = null;
                        dataGridView1.Refresh();
                    }

                    dataGridView1.DataSource = _ds.Tables[0];

                    conn.Close();
                }
            }
        }

        private void QuitProgram(object sender, EventArgs e)
        {
            if (_choice.comboBox1.Text == "English")
            {
                System.Media.SoundPlayer _sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Exclamation.wav");
                _sound.Play();

                DialogResult _dialogResult = MessageBox.Show("Are You Sure Want to Quit?", "Warning", MessageBoxButtons.YesNo);

                if (_dialogResult == DialogResult.Yes)
                {
                    this.Hide();

                    this.Close();
                }

                else
                {

                }
            }

            else if (_choice.comboBox1.Text == "Indonesian")
            {
                System.Media.SoundPlayer _sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Exclamation.wav");
                _sound.Play();

                DialogResult _dialogResult = MessageBox.Show("Apakah Kamu Benar-benar mau Keluar?", "Warning", MessageBoxButtons.YesNo);

                if (_dialogResult == DialogResult.Yes)
                {
                    this.Hide();

                    this.Close();
                }

                else
                {

                }
            }
        }

        private void Clear(object sender, EventArgs e)
        {
            if (_choice.comboBox1.Text == "English")
            {
                System.Media.SoundPlayer _sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Exclamation.wav");
                _sound.Play();
                DialogResult _dialogResult = MessageBox.Show("Are You Sure Want to Clear all the Data?", "Warning", MessageBoxButtons.YesNo);

                if (_dialogResult == DialogResult.Yes)
                {
                    ClearDatabase(sender, e);
                }

                else
                {

                }
            }

            else if (_choice.comboBox1.Text == "Indonesian")
            {
                System.Media.SoundPlayer _sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Exclamation.wav");
                _sound.Play();
                DialogResult _dialogResult = MessageBox.Show("Apakah Kamu Yakin mau Menghapus semua Data?", "Warning", MessageBoxButtons.YesNo);

                if (_dialogResult == DialogResult.Yes)
                {
                    ClearDatabase(sender, e);
                }

                else
                {

                }
            }
        }

        private void ClearDatabase(object sender, EventArgs e)
        {
            using (OleDbConnection conn = new OleDbConnection(connectionString))
            {
                string query = "DELETE FROM [TransRecord]";

                conn.Open();

                using (OleDbDataAdapter _adapter = new OleDbDataAdapter(query, conn))
                {
                    _ds.Clear();
                    _adapter.Fill(_ds, "TransRecord");
                    dataGridView1.DataSource = null;
                    dataGridView1.Refresh();
                }

                dataGridView1.DataSource = _ds.Tables[0];

                conn.Close();
            }

            if (_choice.comboBox1.Text == "English")
            {
                System.Media.SoundPlayer _sounds = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Exclamation.wav");
                _sounds.Play();
                MessageBox.Show("Cleared!", "Cleared");
            }

            else if (_choice.comboBox1.Text == "Indonesian")
            {
                System.Media.SoundPlayer _sounds = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Exclamation.wav");
                _sounds.Play();
                MessageBox.Show("Berhasil Dibersihkan!", "Cleared");
            }
        }

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams myCp = base.CreateParams;
                myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON;
                return myCp;
            }
        }

    }

以下是截图:

enter image description here

在上面的图片中,当表单加载时,组合框1中的日期是9/22/2013。

enter image description here

在上图中,我只是想将组合框1中的日期更改为9/24/2013,但它也改变了组合框2中的日期。

如何解决?感谢

1 个答案:

答案 0 :(得分:1)

这是因为您对DataSource使用相同的_datetime comboboxes

comboBox1.DataSource = _dateTime;
comboBox2.DataSource = _dateTime.ToList();

但是我认为_dateTime可能不是你想要为两个组合框设置为DateSource的,如果是这样,只需留下一些评论。