每当我更改组合框1时,组合框2中的日期总是更改

我的程序中有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中的日期为9/22/2013。

在此处输入图片说明

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

如何解决?谢谢

国王王

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

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

每当我更改组合框1时,组合框2中的日期总是更改

来自分类Dev

更改组合框时更新QAbstractTableModel

来自分类Dev

更改组合框边框颜色-更改SelectedIndex时闪烁

来自分类Dev

UWP打开时更改组合框位置

来自分类Dev

如何更改组合框WPF的CornerRadius

来自分类Dev

更改组合框的样式和颜色

来自分类Dev

更改组合框JAVA的标题

来自分类Dev

如何更改组合框背景

来自分类Dev

更改组合框中“悬停的”项目的文本颜色?

来自分类Dev

在delphi firemonkey mobile中更改组合框的字体颜色

来自分类Dev

更改组合框中某些行的背景颜色

来自分类Dev

在Winform应用程序中更改组合框的高度

来自分类Dev

在WPF中更改组合框的边框颜色

来自分类Dev

在datagridview中更改组合框单元格背景

来自分类Dev

在 QML 中更改组合框颜色的正确方法

来自分类Dev

更改组合框的ListItem文本而不更改其值

来自分类Dev

更改组合框的颜色(显示箭头按钮的组合框的标题框)

来自分类Dev

更改组合框文本,但未选择项目

来自分类Dev

WPF更改组合框的背景色

来自分类Dev

Tkinter如何更改组合框小部件箭头图像

来自分类Dev

wpf datagrid:动态更改组合框集合

来自分类Dev

如何更改组合框C#mysql的起始位置

来自分类Dev

ExtJS 动态更改组合框项的值

来自分类Dev

每当我在jQuery中更改新创建的选择框的值时,如何添加新的选择框?

来自分类Dev

样式为下拉列表时更改组合框的完整背景色

来自分类Dev

如何在Windows 8应用商店中更改组合框箭头的颜色

来自分类Dev

WPF-更改组合框中的枚举的字符串值

来自分类Dev

MS Access Forms:如何动态更改组合框中的选择选项?

来自分类Dev

WPF-更改组合框中的枚举的字符串值

Related 相关文章

  1. 1

    每当我更改组合框1时,组合框2中的日期总是更改

  2. 2

    更改组合框时更新QAbstractTableModel

  3. 3

    更改组合框边框颜色-更改SelectedIndex时闪烁

  4. 4

    UWP打开时更改组合框位置

  5. 5

    如何更改组合框WPF的CornerRadius

  6. 6

    更改组合框的样式和颜色

  7. 7

    更改组合框JAVA的标题

  8. 8

    如何更改组合框背景

  9. 9

    更改组合框中“悬停的”项目的文本颜色?

  10. 10

    在delphi firemonkey mobile中更改组合框的字体颜色

  11. 11

    更改组合框中某些行的背景颜色

  12. 12

    在Winform应用程序中更改组合框的高度

  13. 13

    在WPF中更改组合框的边框颜色

  14. 14

    在datagridview中更改组合框单元格背景

  15. 15

    在 QML 中更改组合框颜色的正确方法

  16. 16

    更改组合框的ListItem文本而不更改其值

  17. 17

    更改组合框的颜色(显示箭头按钮的组合框的标题框)

  18. 18

    更改组合框文本,但未选择项目

  19. 19

    WPF更改组合框的背景色

  20. 20

    Tkinter如何更改组合框小部件箭头图像

  21. 21

    wpf datagrid:动态更改组合框集合

  22. 22

    如何更改组合框C#mysql的起始位置

  23. 23

    ExtJS 动态更改组合框项的值

  24. 24

    每当我在jQuery中更改新创建的选择框的值时,如何添加新的选择框?

  25. 25

    样式为下拉列表时更改组合框的完整背景色

  26. 26

    如何在Windows 8应用商店中更改组合框箭头的颜色

  27. 27

    WPF-更改组合框中的枚举的字符串值

  28. 28

    MS Access Forms:如何动态更改组合框中的选择选项?

  29. 29

    WPF-更改组合框中的枚举的字符串值

热门标签

归档