2015-08-24 13 views
-1

我的Excel是:我在Excel中2個colums但我想導入某些部分以C#程序

 
region   city 
iç anadolu  ankara 
iç anadolu  ankara 
iç anadolu  ankara 
iç anadolu  ankara 
iç anadolu  kayseri 
marmara   istanbul 
marmara   istanbul 
marmara   istanbul 
marmara   bursa 
marmara   bursa

我想的是,當combobox1選擇iç anadolu,只有ankarakayseri都出現在combobox2和當在組合框1中選擇marmara時,組合框2中僅出現istanbulbursa

我該怎麼做?

這是我的代碼:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Data.OleDb; 
namespace trfd 
{ 
public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
} 
    OleDbConnection baglan = new OleDbConnection(@"Provider =Microsoft.ACE.OLEDB.12.0;Data Source=C:\\users\\toshiba\\desktop\\proje_ofy\\proje-1; Extended Properties='Excel 12.0 xml;HDR=YES;'");  
    private void Form1_Load(object sender, EventArgs e) 
    { 
     baglan.Open(); 
     string sql = "SELECT * FROM [Sheet1$A1:A100]"; 
     OleDbCommand komut = new OleDbCommand(sql, baglan); 
     OleDbDataReader dr = null; 
     dr = komut.ExecuteReader();     
     while (dr.Read()) { 
      if (!comboBox1.Items.Contains(dr[0].ToString())) 
       { 
     comboBox1.Items.Add(dr[0].ToString());     
      }     

}

} 
    private void button1_Click(object sender, EventArgs e) 
    {   
    } 
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
    {   
    } 
    private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) 
    {    
    } 

}}

+0

您所提供的代碼無關和你的 問題(除了在加載時填充組合框),你有什麼嘗試? – Sayse

+0

button1是不必要的。我知道我們在combobox1_selected.ndexcanged中寫了一些東西。但我不知道我寫什麼來完成我的要求。所以他們是空的。 –

回答

0

按照下面的代碼

public Form1() 
{ 
    InitializeComponent(); 

    OleDbConnection baglan = new OleDbConnection(@"Provider =Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\d.faldu\Desktop\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\tempExcel; Extended Properties='Excel 12.0 xml;HDR=YES;'"); 

    baglan.Open(); 
    string sql = "SELECT * FROM [Sheet1$A1:A100]"; 
    OleDbCommand komut = new OleDbCommand(sql, baglan); 
    OleDbDataReader dr = null; 
    dr = komut.ExecuteReader(); 
    while (dr.Read()) 
    { 
     if (!comboBox1.Items.Contains(dr[0].ToString())) 
     { 
      comboBox1.Items.Add(dr[0].ToString()); 
     } 
    } 
    baglan.Close(); 
} 

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    comboBox2.Items.Clear(); 

    OleDbConnection baglan = new OleDbConnection(@"Provider =Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\d.faldu\Desktop\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\tempExcel; Extended Properties='Excel 12.0 xml;HDR=YES;'"); 
    baglan.Open(); 
    string sql = "SELECT * FROM [Sheet1$B1:B100]"; 
    OleDbCommand komut = new OleDbCommand(sql, baglan); 
    OleDbDataReader dr = null; 
    dr = komut.ExecuteReader(); 

    string sql_2 = "SELECT * FROM [Sheet1$A1:A100]"; 
    OleDbCommand komut_2 = new OleDbCommand(sql_2, baglan); 
    OleDbDataReader dr_2 = null; 
    dr_2 = komut_2.ExecuteReader(); 
    while (dr_2.Read() && dr.Read()) 
    { 
     if (dr_2[0].ToString() == comboBox1.SelectedItem.ToString()) 
     { 
      if (!comboBox2.Items.Contains(dr[0].ToString())) 
      { 
       comboBox2.Items.Add(dr[0].ToString()); 
      } 
     } 
    } 
    baglan.Close(); 
} 
+0

謝謝,這對我很好:D –

+0

如果我的回答有幫助,請發起投票! :) –

相關問題