2017-10-16 79 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; 
      } 
     } 
    } 
} 
相關問題