0

I know this is duplicate question. I have try finding the solution in duplicate question but I failed.

The situation is I have 2 combo box (Telerik Winforms) called ComboBranch and ComboPanel. ComboPanel will show different value when user select the some value in ComboBranch.

So this is the code

 private void tbDropBranch_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
    {
        dataPanel();
    }

    void dataPanel()
    {

        DataTable dtPanel = dataBinding._valuePanel(Convert.ToInt32(tbDropBranch.SelectedValue.ToString())); // Error in here
        tbDropPanel.DataSource = new BindingSource(dtPanel, null);
        tbDropPanel.DisplayMember = "panelName";
        tbDropPanel.ValueMember = "panelID";

    }

UPDATE

If I do Event tbDropBranch_Leave it`s work. But why I got error when I use tbDropBranch_SelectedIndexChanged ?

 private void tbDropBranch_Leave(object sender, EventArgs e)
        {

            dataPanel();

        }

SOLUTION

I just do like this :

void getIdBranch()
    {

        if ("System.Data.DataRowView" == tbDropBranch.SelectedValue.ToString())
        {
            return;
        }
        else
        {

            DataTable dtPanel = dataBinding._valuePanel(Convert.ToInt32(tbDropBranch.SelectedValue.ToString()));
            tbDropPanel.DataSource = new BindingSource(dtPanel, null);
            tbDropPanel.DisplayMember = "panelName";
            tbDropPanel.ValueMember = "panelID";
        }

    }

Thank to those who helped.. :)

17
  • What is the value of tbDropBranch.SelectedValue.ToString()? Commented Oct 24, 2012 at 16:19
  • 1
    And what exactly is dataBinding._valuePanel ? Commented Oct 24, 2012 at 16:20
  • @JonB - It will give branchID value. If I create event tbDropBranch_leave, Everything is ok. But event tbDropBranch_SelectedIndexChanged I got that error Commented Oct 24, 2012 at 16:24
  • do you have null value there or string.Empty value..? Commented Oct 24, 2012 at 16:25
  • 1
    No problem, at least you try to help. Thanks mate.. Commented Oct 24, 2012 at 16:50

3 Answers 3

1

I believe that you are receiving an exception because tbDropBranch.SelectedValue.ToString() can not be converted into an Integer or Int. I'd recommend you to insert the following line first to make sure if the string can be parsed before executing to avoid receiving exceptions. It'll be also appreciated if you could provide the exact exception you are receiving.

   int x = 0;
   void dataPanel()
    {
        if (Int32.TryParse(tbDropBranch.SelectedValue.ToString(), out x)) //Check if tbDropBranch.SelectedValue.ToString() is a valid integer
        {
          DataTable dtPanel = dataBinding._valuePanel(Convert.ToInt32(tbDropBranch.SelectedValue.ToString())); // Error in here
          tbDropPanel.DataSource = new BindingSource(dtPanel, null);
          tbDropPanel.DisplayMember = "panelName";
          tbDropPanel.ValueMember = "panelID";
        }
    }

You can always try the following to get the exact value of the current selected item based on its index

if (ComboBranch.SelectedIndex != -1) // Execute the following only if there's a selected index
   {
        ComboBranch.Items[ComboBranch.SelectedIndex].ToString(); // Get the value of the selected index
   }

Thanks
I hope you find this helpful :)

Sign up to request clarification or add additional context in comments.

2 Comments

Nice man.. Your idea same with me. But my idea is if ("System.Data.DataRowView" == tbDropBranch.SelectedValue.ToString()){return;}. Hehe
@Chuki2 I'm glad you've found an answer to your question. Have a great day :)
1

This is because tbDropBranch.SelectedValue.ToString() returns System.Data.DataRowView. Convert.ToInt32 can't turn that value into an int. You'll need to pass in something else.

Comments

0

I doubt dataBinding._valuePanel uses an Int32 as its parameter... maybe it should be an int?

dataBinding._valuePanel(int.Parse(tbDropBranch.SelectedValue.ToString()));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.