2017-10-16 77 views
-6

我有一个组合框填充mysql数据库。 我想在用户从组合框中选择一年时间后,在想要单击按钮并移至下一年后,创建两个靠近组合框的按钮。如何从数据库c创建下一个和上一个按钮年#

现在我有了这个代码来从数据库中获得数年。

 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?

回答

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; 
      } 
     } 
    } 
} 
相关问题