Передача значения подстроки gridview в текстовое поле - PullRequest
0 голосов
/ 28 сентября 2018

Пожалуйста, предложите мне любую идею передать значение подстроки вида сетки в текстовом поле.

frm.txtcustcode.text = dg.cells ["custcode"]. Value.ToString (). Trim ();

Мне нужно передать (0000001234) "01234" в txtcustcode.Text;

Ответы [ 2 ]

0 голосов
/ 28 сентября 2018

Ниже код может работать нормально:

frm.txtcustcode.Text= dg.Cells["CustomerCode"].Value.ToString().Trim().Substring(5,5);
0 голосов
/ 28 сентября 2018

Надеюсь, я правильно понял ваш вопрос.Код:

dg.cells["custcode"].value.ToString().Trim();

- это значение в сетке, и значение, полученное с помощью кода: 0000001234 Вы хотите отобразить это значение в текстовом поле в форме как значение: 01234

Если это так, вы можете сделать следующее:

    //If the value of custcode always starts with 0's but wont contain 0's in the number.
    String sCustCode = "0000001234";

    sCustCode = sCustCode.Replace("0", "");
    sCustCode = "0" + sCustCode;

    MessageBox.Show(sCustCode); //Displays 01234

    //Or if your CustCode can contain a 0 and always starts with 0's.

    sCustCode = "0000001234";
    int num = Int32.Parse(sCustCode);  //Depending on the number format use the correct cast.
    sCustCode = "0" + num.ToString();

    MessageBox.Show(sCustCode); //Displays 01234

    //Or you dont want to use a Cast to get your code in the fear of dataloss for some reason.
    sCustCode = "0000001234";

    int index = 0;
    int position = 0;
    foreach(char myChar in sCustCode)
    {
        if (myChar != '0')
        {
            position = index;
            break;
        }
        index++;

    }

    if (position == 0)
    {
        //No starting 0 found
    }
    else
    {
        sCustCode = sCustCode.Substring(position - 1, sCustCode.Length - position +1);
    }

    MessageBox.Show(sCustCode); //Displays 01234  

Я уверен, что есть более эффективные способы сделать это, это зависит от цели.Надеюсь, что это поможет вам в правильном направлении.

...