选择组合框中的项目并将其删除

时间:2009-08-19 15:55:00

标签: c# combobox

我想在文本框的文本中使用数字找到组合框的索引,然后将其删除。填充组合框的项目属于数据库,因此我使用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");
        }
    }

3 个答案:

答案 0 :(得分:0)

有几件事情浮现在脑海中。

要么tNumEditBox3.Text中的值不是组合框中存在的值。你有没有在调用之前仔细检查它的值:

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

另一种选择是radioButton2.Checkedfalse

顺便说一句,您不需要针对truefalse显式测试布尔值。你可以写:

if (boolean_value)
{
    // Do stuff
}

答案 1 :(得分:0)

您对FindStringExact的来电将跳过第一项。除非您希望它仅在第一个之后搜索项目,否则您应该使用不带startIndex参数的重载,如下所示:

buscar = comboBox1.FindStringExact(tNumEditBox3.Text);

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

答案 2 :(得分:0)

根据我的理解(如果我错了,请更正我),您的文本框中包含组合框中项目的ID(例如3)。

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

comboBox1.SelectedItem = 
    cambiosEnviosDataSet.CambioG.FindByCambioGID(Convert.ToInt16(tNumEditBox3.Text));
相关问题