将 gridview 的子字符串值传递给文本框

Passing substring value of gridview to textbox

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

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

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

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

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  

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

以下代码可能工作正常:

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