-1

我有问题。如何在组合框中添加项目?

我已经尝试过这段代码:

comboBox1.Items.Add("--Dates--");
comboBox1.SelectedIndex = 0;

但是当我运行程序时,它无法在组合框中添加项目。

这是代码:

public partial class Trans : Form
    {
        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> _startDate = new List<DateTime>();
        private List<DateTime> _endDate = new List<DateTime>();

        int startDate;
        int endDate;

        public Trans()
        {
            InitializeComponent();
        }

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

        private void Trans_Load(object sender, EventArgs e)
        {
            startDate = (int)DateTime.Today.AddYears(5).Subtract(DateTime.Today).TotalDays + 1;
            endDate = (int)DateTime.Today.AddYears(5).Subtract(DateTime.Today).TotalDays + 1;

            for (int i = 0; i < startDate; i++)
            {
                _startDate.Add(DateTime.Today.AddDays(i));
            }

            for (int i = 0; i < endDate; i++)
            {
                _endDate.Add(DateTime.Today.AddDays(i));
            }

            StartDateCollection(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)
        {
            comboBox1.DataSource = _startDate;
            comboBox1.FormatString = "M/dd/yyyy";
            comboBox1.FormattingEnabled = true;
        }

        private void StartDateCollection_SelectedIndexChanged(object sender, EventArgs e)
        {
            DateTime comboBox1_SelectedDate = Convert.ToDateTime(comboBox1.SelectedValue);
            List<DateTime> tempDate = _endDate.Where(d => d > comboBox1_SelectedDate).ToList<DateTime>();
            comboBox2.DataSource = tempDate;
            comboBox2.FormatString = "M/dd/yyyy";
            comboBox2.FormattingEnabled = true;
        }

(....其他代码)

我把 Items.Add 放在 StartDateCollection 函数中,但它没有出现我添加的字符串,只出现日期。

我该如何解决?

谢谢。

或者,我想拥有从第一天起的交易记录。

示例:我有今天的交易记录24 September 2013,但是明天打开程序时,我无法打开当天的交易记录24 September 2013,因为日期24 September 2013已经消失了。我希望组合框项目从交易记录的第一个日期开始显示。

非常感谢您的回答非常感谢!

这是屏幕截图: 在此处输入图像描述

从上图中可以看出,ComboBox 中的开始日期是Date,我想在显示---Dates---之前创建一个Date。所以它会是这样的(在组合框中)---日期--- 9/24/2013 9/25/2013 ......(等等)

4

2 回答 2

1

如果您需要组合框中的第一个项目应该是一些通用文本,例如“选择日期”或其他内容,以便可以使用。

    private void button4_Click(object sender, EventArgs e)
    {

        List<DateTime> lstDate = new List<DateTime>();
        DateTime dt = DateTime.Now;
        for (int i = 0; i < 20; i++)
        {
            lstDate.Add(dt);
        }

        List<string> lstDataSource = lstDate.Select(a => a.ToString("M/dd/yyyy")).ToList();
        lstDataSource.Insert(0, "---Select Date---");
        comboBox1.DataSource = lstDataSource;            
        comboBox1.FormattingEnabled = true;

    }
于 2013-09-24T12:57:14.937 回答
0
    private void button4_Click(object sender, EventArgs e)
    {

        List<DateTime> lstDate = new List<DateTime>();
        DateTime dt = DateTime.Now;
        for (int i = 0; i < 40; i++)
        {
            lstDate.Add(dt);
        }

        comboBox1.DataSource = lstDate;
        comboBox1.FormatString = "M/dd/yyyy";
        comboBox1.FormattingEnabled = true;

    }

它在组合框中添加日期

在此处输入图像描述

于 2013-09-24T11:51:09.623 回答