1

I want to seperate multiple values from a gridview control and show it in four textboxes. Is that possible? Right now I get this value: enter image description here

With this code:

        var lblRef = new Label
                         {
                             Text = ((Label) row.FindControl("LabelAssignmentReference")).Text
                         };

        string valueTextBox = lblRef.Text;
        int indexOfRefSwe = valueTextBox.IndexOf(",", StringComparison.Ordinal);
        string valueRef = valueTextBox.Substring(0, indexOfRefSwe);

        TextBoxReference.Text = valueRef;

But how do i get it in multiple values? ` TextBoxReference.Text = valueRef;

        TextBoxRefPhone.Text = "??";
        TextBoxRefEmail.Text = "??";
        TextBoxRefDesc.Text = "??";`
1
  • lblRef.Split(new [] { ',' }); will return an array of the values Commented May 10, 2013 at 9:45

3 Answers 3

4

This should get you started.

string[] splits = lblRef.Text.Split(',');
Console.WriteLine(splits[0]); // refname
Console.WriteLine(splits[1]); // 08712332
Console.WriteLine(splits[2]); // [email protected]
Console.WriteLine(splits[3]); // refdescription

I suggest also adding validation checks to make sure you don't get any errors, such as checking that splits.Length == 4 as expected.

Note that the spaces will be included in the beginning of the last three elements of splits. You can eliminate those using the Trim method, or by providing an array of delimiters new[] {',', ' '} to the split function and ignore empty elements (there's an overload for that).

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

1 Comment

I've tried this one but filled in my textboxes instead of console.writeline. .Trim() afterwards. Thanks !
1

There is System.String.Split()-method:

string[] parts = str.Split(new char[] {','});

Afterwards, work on the parts.

Example from MSDN

using System;

public class SplitTest {
    public static void Main() {

        string words = "This is a list of words, with: a bit of punctuation" +
                       "\tand a tab character.";

        string [] split = words.Split(new Char [] {' ', ',', '.', ':', '\t' });

        foreach (string s in split) {

            if (s.Trim() != "")
                Console.WriteLine(s);
        }
    }
}

Comments

0

you can do as below

var values = lblRef.Text.Split(',');

TextBoxRefPhone.Text = values[0];
if(values.Length>0)
   TextBoxRefEmail.Text =values[1];
if(values.Length>1)
  TextBoxRefDesc.Text = values[2];

Edit

there is a Split overload method which accept params. so we can give one character

public string[] Split(params char[] separator);

The params keyword lets you specify a method parameter that takes an argument where the number of arguments is variable.

4 Comments

Split requires an array of values.
@stuartd Wrong. You can provide a single delimiter without an array. The method has many overloads.
To clarify, split will accept a single char value, but if you want to use a string, then it has be passed in an array.
@stuartd params for that. I think you learn few things today :)

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.