2

I have a combo box which have item strings like:

1 .  Apple
2 .  Banana
3 .  Mango 

1,2,3 are category id & Apple, Banana, Mango is Category Name.

I want to know Category Id From comboBox using Category name which is sub-string of ComboBox item.

example:

I want to know the Category Id of Banana. which is 2.

Any help ?

5
  • 3
    What did you try so far? Commented Aug 24, 2012 at 9:32
  • 1
    is that work for you ?i.e my answer ? Commented Aug 24, 2012 at 10:09
  • Thanks to all of you especially Pranay Rana Your answer is really useful. that is my first forum & second question this forum is really so active. Commented Aug 24, 2012 at 10:26
  • 1
    -1 for unclear question. you should write your question clearly and complete. like what you commented on Schaliasos's answer Commented Aug 24, 2012 at 10:29
  • @hamed thanks next time i will explain completely Insha Alah Commented Aug 24, 2012 at 11:02

5 Answers 5

4

Use this code to an event that should be after you selected the item in comboBox :

        string []str;
        str = comboBox1.Text.Split(' ');
        string categoryId = str[0];
Sign up to request clarification or add additional context in comments.

2 Comments

becuse how i get to know str[0] is related to Banana or Apple or someother string...answer is incomplete
@PranayRana I thought he wants to fetch the ID of category after selecting it on comboBox.
3

Try the following code. It will give the CategotyId of the selected Category.

private void ComboBox1_SelectedIndexChanged(object sender, System.EventArgs e) {
    string selectedText = comboBox1.SelectedText;
    string categoryId  = selectedText.Substring(0, selectedText.IndexOf(" "));

    MesasgeBox.Show(categoryId);
}

4 Comments

i am not selecting any index of combobox & not selectedIndexChanged method is called. Combo box is filled with values & now i am selecting these strings like Mango from grid box instead of again fetching category id from data base which is costly in term of processing cost i wants to take that value from combobox which is already filled & much faster. thanks For Answer
The point of my answer is to use the Substring method. You can use it in any method you want. I don't understand the reason of downvoting.
i did not downvoted for you. Your answer was helpful really thanks Schaliasos.
Pranay Rana downvoted you. the reason is as what is he commented on my answer
2
    foreach (object item in cmb.Items)
    {
      string[] str = item.ToString().split(new char[] {' '}
, StringSplitOptions.RemoveEmptyEntries);
      if(str[1] == "Banana")
      {
           Console.Write(str[0]);
      }
    }

2 Comments

There is 3 space between category ID and category name.it should be str[str.count-1] == "Bannana"
@hamed - that y i added StringSplitOptions.RemoveEmptyEntries in my spilt function as parameter so it remove null entries..read the documenttation of split that will give you more idea
2

@Pranay Rana Your Answer Helped me: I wrote my method like that

private string get_Godown_id(string godown_name)
    {
        foreach (object item in cb_send_to.Items)
        {
            if (item.ToString().Split('.')[1].Trim() == godown_name)
            {
                return (item.ToString().Split('.')[0]);
            }
        }
        return "";
    }

Comments

1
foreach (object item in cb_send_to.Items)
    {
        if (item.ToString().Split('.')[1].Trim() == godown_name)
        {
            return (item.ToString().Split('.')[0]);
        }
    }

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.