如何从数据库c#创建多年的下一个和上一个按钮

时间:2017-10-16 09:01:48

标签: c# mysql .net

我有一个填充了mysql数据库的组合框。 当用户从组合框中选择一年后,我想在组合框附近创建两个按钮,之后我想点击按钮并移动到下一年。

现在我有了这段代码来从DB中获得多年。

      private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

        string command1 = "select year(Dat) FROM hydgod where Station=" + comboBox1.Text;

        MySqlDataAdapter da1 = new MySqlDataAdapter(command1, connection);
        DataTable dt1 = new DataTable();
        da1.Fill(dt1);

        comboBox2.Items.Clear();
        comboBox2.SelectedItem = -1;


        foreach (DataRow row in dt1.Rows)
        {
            string rowz = string.Format("{0}", row.ItemArray[0]);
            comboBox2.Items.Add(rowz);
            comboBox2.AutoCompleteCustomSource.Add(row.ItemArray[0].ToString());
        }
    }

我怎样才能从组合框中获得选定的年份,并在明年增加+1,并以-1减少为前一年?

1 个答案:

答案 0 :(得分:0)

请参阅下面的代码,了解上一个和下一个按钮

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        int comboboxIndex = 0;
        public Form1()
        {
            InitializeComponent();

            comboBox1.Items.Clear();
            for (int i = 2000; i < 2018; i++)
            {
                comboBox1.Items.Add(i.ToString());
            }
            comboBox1.SelectedIndex = 0;
        }

        private void Previous_Click(object sender, EventArgs e)
        {
            if (comboboxIndex > 0)
            {
                comboboxIndex--;
                comboBox1.SelectedIndex = comboboxIndex;
            }

        }

        private void Next_Click(object sender, EventArgs e)
        {

            if (comboboxIndex < comboBox1.Items.Count - 1)
            {
                comboboxIndex++;
                comboBox1.SelectedIndex = comboboxIndex;
            }
        }
    }
}