我如何在组合框中添加项目?

时间:2013-09-24 11:17:01

标签: c# winforms ms-access combobox

我遇到了问题。如何在comboBox中添加项目?

我已经尝试过这段代码:

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

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

以下是代码:

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已经消失了。我想从交易记录的第一个日期开始显示组合框项目。

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

以下是截图: enter image description here

从上图中可以看出,ComboBox中的开始日期为Date,我希望在显示---Dates---之前创建Date。所以它会是这样的(在comboBox中) - -日期 - - 2013年9月24日 2013年9月25日 ...... (依此类推)

2 个答案:

答案 0 :(得分: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;

    }

答案 1 :(得分: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;

    }

它在组合框中添加日期

enter image description here