简体   繁体   English

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

[英]Filtering combobox with another combobox value

I have three combo-box which filtering value in different tables. 我有三个组合框,它们在不同的表中过滤值。 For the first two combo-box i have no problem but for the third combo-box, I got error show input strings was not in correct format . 对于前两个组合框,我没有问题,但是对于第三个组合框,出现错误,显示input strings was not in correct format I using the same code for the other two and it working correctly. 我为其他两个使用了相同的代码,并且可以正常工作。 Can someone specify how to troubleshoot this problem? 有人可以指定如何解决此问题吗?

Here my code:- 这是我的代码:-

This one is for combobox two which worked perfectly:- 这个适用于完美结合的组合框二:

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

This is the code which show error 这是显示错误的代码

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

I hope someone can show me how to rectify this problem. 我希望有人可以向我展示如何纠正此问题。 Thanks. 谢谢。

***UPDATE**** ***更新****

Here the full code 这是完整的代码

 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);
        }
    }

Check if this can be converted to int first like this: 首先检查是否可以将其转换为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);
    }
}

Or using the SelectedIndex property: 或使用SelectedIndex属性:

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

What are the cbPier.DisplayMember and cbPier.ValueMember ? 什么是cbPier.DisplayMembercbPier.ValueMember Is the diplay member the string and value member the int? diplay成员是字符串,value成员是int吗? Double check if that's the case first ... 仔细检查是否首先是这种情况...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM