Click here to Skip to main content
15,894,106 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Friend

i have an dynamic Dropdown box with the value like
1) 100 Gm
2) 250 Gm
3) 500 Gm
4) 1 KiloGram

now i want to split this value into two variable like 100 is in INT type and Gm is in String Type...
How can i do that .....


Thank You All...
Posted
Comments
Kenneth Haugland 24-Aug-12 10:22am    
String.Split(" ") ?

The split method of the string class will turn your string in to a string array. If you split on the space, you can use int.TryParse to get the int value out and the second value will just be the unit of measure.

You could also set the value of each item in the drop list to be the value in grams. By which I mean, display what you're showing, but there's a hidden 'value' field you can store a normalised value in.
 
Share this answer
 
v4
C#
string values[]  = Regex.Split(lineValue," ");

int amount = int.Parse(values[0]);
string unit = values[1];
 
Share this answer
 
v2
Comments
Christian Graus 24-Aug-12 10:43am    
you should never assume a collection has more than one element in it, without checking first. It will blow up
ZurdoDev 24-Aug-12 10:52am    
I thought you were all about helping people learn instead of doing everything for them? What you have pointed out is basic, but correct.
Christian Graus 24-Aug-12 11:08am    
Yes, I just explained to him the issue of his code. I think you need psychiatric help
ZurdoDev 24-Aug-12 11:13am    
"I think you need psychiatric help" Can I get your doctor's number? :)
Christian Graus 24-Aug-12 11:18am    
You know, I regret saying that. How about, if you want to abuse me for being smarter than you, we do it in the soapbox and not pollute this forum ?
Here is one approach.

Aspx page.

XML
<asp:DropDownList ID="ddlWeight" runat="server">
<asp:ListItem Text="100 Gm" Value="100 Gm"></asp:ListItem>
<asp:ListItem Text="250 Gm" Value="250 Gm"></asp:ListItem>
<asp:ListItem Text="500 Gm" Value="500 Gm"></asp:ListItem>
<asp:ListItem Text="1 KiloGram" Value="1 KiloGram"></asp:ListItem>


Code behind

C#
int Quantity;
bool Result;


string[] strUnit = new string[ddlWeight.Items.Count];
int[] intQuantity = new int[ddlWeight.Items.Count];

for (int i = 0; i < ddlWeight.Items.Count; i++)
{
    string[] StrSplitResult = ddlWeight.Items[i].Text.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

    if (StrSplitResult.Length > 0)
    {
        Result = int.TryParse(StrSplitResult[0].Trim(), out Quantity);

        if (Result)
        {
            intQuantity[i] = Quantity;
        }
    }

    if (StrSplitResult.Length > 1)
    {
        strUnit[i] = StrSplitResult[1].Trim();

    }
}
 
Share this answer
 
Comments
Yatin chauhan 25-Aug-12 0:54am    
I dont have dropdown.. i have bind that dropdown dynamic which already store in database in two column like:-
Quantity Unit
100 Gram
200 Gram
300 Gram
1 Kilogram

i bound in dropdown with concate.
__TR__ 25-Aug-12 14:26pm    
The above code (in code behind) will be the same irrespective of whether the list items are coming from database or entered in the aspx page. The solution focuses on how to split the list items in dropdown because that's what you asked and not on how to display data from database in a dropdown list. You can use the above piece of code after you bind your dropdown to a datasource.
There are several ways to do it. One easy way would be:

C#
String tempValue = < dropdownlistvalue >
Int32 numberPart = tempValue.Split(' ')[0];
String textPart = tempValue.Split(' ')[1];


Use the Split() method on a string. You get it a character to split on and then you can reference them by index.
 
Share this answer
 
v2
Comments
RAKESH CHAUBEY 24-Aug-12 10:31am    
ryanb31 .Can U plz Provide a small example With Code view and post it ..just take 2 drpdown and Show a very simple example of that ..i also have same doubt
ZurdoDev 24-Aug-12 10:40am    
That is a code example that I have.
Christian Graus 24-Aug-12 10:42am    
You would be better off using the value field of the control, than string mashing. Ryan is just repeating the worse of the two suggestions I made.
ZurdoDev 24-Aug-12 10:46am    
I answered the question. I have had times where I need 2 IDs and so I will put them both into the Value field and parse it. Some real world experience might help you out some.
Christian Graus 24-Aug-12 10:48am    
I'm sorry, I'm not going to argue with someone so foolish as to think that calling split twice is real world code, or that string mashing is better than a clean way of storing the values you need.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900