【问题标题】:Passing substring value of gridview to textbox将gridview的子字符串值传递给文本框
【发布时间】:2018-09-28 06:34:43
【问题描述】:

请建议我在文本框中传递网格视图的子字符串值。

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

我需要将 (0000001234) "01234" 传递给 txtcustcode.Text;

【问题讨论】:

    标签: c#-2.0


    【解决方案1】:

    以下代码可能工作正常:

    frm.txtcustcode.Text= dg.Cells["CustomerCode"].Value.ToString().Trim().Substring(5,5);
    

    【讨论】:

      【解决方案2】:

      希望我能正确理解您的问题。 代码:

      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  
      

      我相信有更好的方法可以做到这一点,这取决于目的。 希望这可以帮助您朝着正确的方向前进。

      【讨论】:

        猜你喜欢
        • 2010-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多