使用另一个组合框值过滤组合框

时间:2014-11-25 09:06:48

标签: c# sql winforms combobox

我有三个组合框,它们在不同的表中过滤值。对于前两个组合框我没有问题,但对于第三个组合框,我得到错误显示 input strings was not in correct format 。我对其他两个使用相同的代码并且它正常工作。有人可以指定如何解决这个问题吗?

这是我的代码: -

这是一个完美运作的组合框两个: -

private void cbBridge_SelectedIndexChanged(object sender, EventArgs e)
{
    if (cbBridge.SelectedValue.ToString() != null)
    {
        int BridgeID = Convert.ToInt32(cbBridge.SelectedValue.ToString());
        FillPier(BridgeID);
    }
}

这是显示错误的代码

private void cbPier_SelectedIndexChanged(object sender, EventArgs e)
{
    if (cbPier.SelectedValue.ToString() != null)
    {
        int PierID = Convert.ToInt32(cbPier.SelectedValue.ToString());
        FillDataPoint(PierID);
    }
}

我希望有人能告诉我如何纠正这个问题。感谢。

*** **** UPDATE

这里是完整的代码

 private void FillPier(int BridgeID)
    {
        SqlConnection con = new SqlConnection(conString);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "SELECT PierID, PierName, BridgeID FROM tbPier WHERE BridgeID = @BridgeID";
        cmd.Parameters.AddWithValue("@BridgeID", BridgeID);
        DataSet objDs = new DataSet();
        SqlDataAdapter dAdapter = new SqlDataAdapter();
        dAdapter.SelectCommand = cmd;
        con.Open();
        dAdapter.Fill(objDs);
        con.Close();
        if (objDs.Tables[0].Rows.Count > 0)
        {
            cbPier.DataSource = objDs.Tables[0];
            cbPier.DisplayMember = "PierName";
            cbPier.ValueMember = "PierID";

        }

    }

    private void FillDataPoint(int PierDP)
    {
        SqlConnection con = new SqlConnection(conString);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "SELECT PierID, InspectDate FROM tbDatapoint WHERE PierID = @PierID";
        cmd.Parameters.AddWithValue("@PierID", PierDP);
        DataSet objDs = new DataSet();
        SqlDataAdapter dAdapter = new SqlDataAdapter();
        dAdapter.SelectCommand = cmd;
        con.Open();
        dAdapter.Fill(objDs);
        con.Close();
        if (objDs.Tables[0].Rows.Count > 0)
        {
            cbInspect.DataSource = objDs.Tables[0];
            cbInspect.DisplayMember = "InspectDate";
            cbInspect.ValueMember = "PierID";
        }

    }

    private void ViewBridge_Load(object sender, EventArgs e)
    {
         FillBridge();            

    }

    private void cbBridge_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (cbBridge.SelectedValue.ToString() != null)
        {
            int BridgeID = Convert.ToInt32(cbBridge.SelectedValue.ToString());
            FillPier(BridgeID);
        }
    }


    private void cbPier_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (cbPier.SelectedIndex != 1)
        {
            int PierDP = Convert.ToInt32(cbPier.SelectedValue.ToString());
            FillDataPoint(PierDP);
        }
    }

1 个答案:

答案 0 :(得分:0)

检查这是否可以像这样首先转换为int:

int x = 0
private void cbPier_SelectedIndexChanged(object sender, EventArgs e)
{
    if (Int32.TryParse(cbPier.SelectedValue.ToString(), out x))
    {
        int PierID = Convert.ToInt32(cbPier.SelectedValue.ToString());
        FillDataPoint(PierID);
    }
}

或使用SelectedIndex属性:

if(cbPier.SelectedIndex != -1)
{
   .....
}

cbPier.DisplayMembercbPier.ValueMember是什么? diplay成员是字符串,值成员是int吗?请仔细检查是否是第一个......