1

我的程序中有 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;
            }
        }

    }

这是屏幕截图:

在此处输入图像描述

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

在此处输入图像描述

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

如何解决?谢谢

4

1 回答 1

1

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

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

但是我认为这两个组合框_dateTime可能不是您想要设置DateSource的,如果是这种情况,请留下一些评论。

于 2013-09-22T07:30:59.357 回答