C# 在组合框中选择项目并将其删除

C# 在组合框中选择项目并将其删除,c#,combobox,C#,Combobox,我想使用文本框文本中的数字找到组合框的索引,然后删除它们。填充组合框的项目属于数据库,因此我使用Delete方法删除行 编辑: 我一直在阅读,findstring在项目列表中查找文本,而不是索引。在组合框的索引中是否仍有文本框中的文本 有人能找到这个代码的问题吗 private void button4_Click(object sender, EventArgs e) { int buscar; buscar = comboBox1.FindString

我想使用文本框文本中的数字找到组合框的索引,然后删除它们。填充组合框的项目属于数据库,因此我使用Delete方法删除行

编辑:

我一直在阅读,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");
        }
    }

我突然想到了几件事

要么tNumEditBox3.Text中的值不是组合框中的值。在调用之前,您是否仔细检查了它的值:

buscar = comboBox1.FindStringExact(tNumEditBox3.Text, 0);
另一种选择是radioButton2.Checked为false

顺便说一句,您不需要显式地测试布尔值是否为true或false。你可以写:

if (boolean_value)
{
    // Do stuff
}
您对FindStringExact的调用将跳过第一项。除非您希望它只搜索第一个项目之后的项目,否则应使用不带startIndex参数的重载,如下所示:

buscar = comboBox1.FindStringExact(tNumEditBox3.Text);
comboBox1.SelectedItem = 
    cambiosEnviosDataSet.CambioG.FindByCambioGID(Convert.ToInt16(tNumEditBox3.Text));

如果这不是您的问题,请检查文本框中的文本是否与组合框中的一个项目完全匹配,并确保选中radioButton1。

据我所知,如果我错了,请更正我,您的文本框具有组合框中项目的ID,例如,3

您需要找到具有该ID的项,然后设置组合框的SelectedItem属性,如下所示:

buscar = comboBox1.FindStringExact(tNumEditBox3.Text);
comboBox1.SelectedItem = 
    cambiosEnviosDataSet.CambioG.FindByCambioGID(Convert.ToInt16(tNumEditBox3.Text));

这个问题的措辞非常糟糕。它经过检查,我一直在阅读,findstring在项目列表中查找文本,而不是索引。在组合框的索引中是否还有文本框中的文本?是的,它在组合框中有de ID和item,我尝试了您编写的代码,但没有成功。我也尝试了SelectedValue,但它说无法将类型为“CambioGRow”的对象转换为“System.IConvertible”。