2009-08-19 60 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

相关问题