2009-08-19 72 views
0

我想在文本框的文本中使用數字找到組合框的索引,然後刪除它們。填充組合框的項目屬於數據庫,因此我使用Delete方法刪除行。選擇組合框中的項目並將其刪除

EDITED

我一直在閱讀和FINDSTRING發現項目列表中的文本,而不是指數。無論如何要查找組合框索引中的文本框中的文本?

任何人都可以找到這個代碼的問題?

private void button4_Click(object sender, EventArgs e) 
    { 
     int buscar; 
     buscar = comboBox1.FindStringExact(tNumEditBox3.Text, 0); 

     comboBox1.SelectedIndex = buscar; 

     if (comboBox1.SelectedIndex >= 0 && radioButton1.Checked == true) 
     { 
       CambiosEnviosDataSet.CambioGRow borrarCambioGFila; 
       borrarCambioGFila = cambiosEnviosDataSet.CambioG.FindByCambioGID(Convert.ToInt16(tNumEditBox3.Text)); 

       borrarCambioGFila.Delete(); 

       this.cambioGTableAdapter.Update(this.cambiosEnviosDataSet.CambioG); 

       CambiosEnviosDataSet.CambioERow borrarCambioEFila; 
       borrarCambioEFila = cambiosEnviosDataSet.CambioE.FindByCambioEID(Convert.ToInt16(tNumEditBox3.Text)); 

       borrarCambioEFila.Delete(); 

       this.cambioETableAdapter.Update(this.cambiosEnviosDataSet.CambioE); 
     } 
     else if (comboBox2.SelectedIndex <= 0 && radioButton2.Checked == true) 
     { 
       CambiosEnviosDataSet.EnviosRow borrarEnvioFila; 
       borrarEnvioFila = cambiosEnviosDataSet.Envios.FindByEnvioID(Convert.ToInt16(tNumEditBox3.Text)); 

       borrarEnvioFila.Delete(); 

       this.enviosTableAdapter.Update(this.cambiosEnviosDataSet.Envios); 
     } 
     else 
     { 
      MessageBox.Show("The key you are using is not in the index"); 
     } 
    } 
+0

這個問題是措辭很差。 – SLaks 2009-08-19 16:02:34

+0

我改進了。 – SLaks 2009-08-19 16:05:37

回答

0

有幾件事值得思考。

這是不是在tNumEditBox3.Text值沒有出現在組合框中的值。你有雙重檢查它的調用前值:

buscar = comboBox1.FindStringExact(tNumEditBox3.Text, 0); 

另一種選擇是,radioButton2.Checkedfalse

順便說一句,你不需要明確地測試一個布爾值對truefalse。你可以寫:

if (boolean_value) 
{ 
    // Do stuff 
} 
0

您撥打FindStringExact的電話會跳過第一項。除非你要它只會在第一次之後,搜索的項目,你應該使用不採取startIndex參數超載,像這樣:

buscar = comboBox1.FindStringExact(tNumEditBox3.Text); 

如果這不是你的問題,檢查文本在文本框中與組合框中的其中一個項目完全匹配,並確保已選中radioButton1。

+0

這是檢查,我一直在閱讀和發現字符串找到項目列表中的文本,而不是索引。無論如何要查找組合框索引中的文本框中的文本? – Ricardo 2009-08-19 16:55:03

0

據我瞭解(糾正我,如果我錯了),你的文本框有一個項目的ID在下拉列表(例如,3)。

你需要找到具有該ID的項目,然後設置SelectedItem媒體資源相關聯的組合框的,就像這樣:

comboBox1.SelectedItem = 
    cambiosEnviosDataSet.CambioG.FindByCambioGID(Convert.ToInt16(tNumEditBox3.Text)); 
+0

是的,它的ID和組合框中的項目,我試過你寫的代碼,但它沒有工作。我也嘗試過使用SelectedValue,但它表示無法將類型'CambioGRow'的對象轉換爲'System.IConvertible'。 – Ricardo 2009-08-19 18:29:14