以编程方式向字符串添加动态数量的空格

时间:2015-12-30 13:54:59

标签: c# treeview

我有一个treeview控件,我希望看起来像这样:

enter image description here

只是乱用css和文本字符串,我实际上非常接近。我只是需要一些帮助来克服这条线。

以下是我用来生成树视图的代码:

    void FillTree_Parent()
    {  // fills the parent view of the Tree Action items
        //int RoleID = Convert.ToInt32(ddlRole.SelectedValue);
        using (SqlConnection con4 = new SqlConnection(ConfigurationManager.ConnectionStrings["PBRConnectionString"].ConnectionString))
        {
            try
            {
                SqlCommand cmd2 = new SqlCommand("SELECT [ACCT_GRP], [ACCT_GRP_PK], [ACTIVE_FLG], [LOAD_BY], [LOAD_TIMESTAMP] FROM [ACCT_GRP_LIST] ORDER BY [ACCT_GRP] ASC", con4);
                SqlDataAdapter da = new SqlDataAdapter(cmd2);
                DataSet PrSet = new DataSet();

                da.Fill(PrSet, "ACCT_GRP");
                TreeViewAccts.Nodes.Clear();
                foreach (DataRow dr in PrSet.Tables[0].Rows)
                {
                    DateTime date = DateTime.Parse(dr["LOAD_TIMESTAMP"].ToString());
                    string formatted = date.ToString("MM/dd/yyyy");
                    TreeNode tnParent = new TreeNode();
                // Here is our focus
                    tnParent.Text = dr["ACCT_GRP"].ToString().Replace("'", "''") + 
                    " ········· " + "Active:" + dr["ACTIVE_FLG"].ToString() + 
                    " ········· " + "Loaded On:" + formatted + "";
                //
                    tnParent.Value = dr["ACCT_GRP_PK"].ToString();
                    tnParent.PopulateOnDemand = true;
                    tnParent.SelectAction = TreeNodeSelectAction.SelectExpand;
                    TreeViewAccts.Nodes.Add(tnParent);
                    FillTree_Child(tnParent, tnParent.Value);
                }
            }
            catch (Exception ae)
            {
                Response.Write(ae.Message);
            }
        }

    }

在标有“//这是我们的焦点”的那个方块中,我需要做的是弄清楚如何获得第一组“·········”以生成动态数量的空格因为dr [“ACCT_GRP”]最多可以包含75个字符。所以,我需要确定dr [“ACCT_GRP”]的长度,从75中减去它,然后生成那么多空格。

任何人都可以帮我解释这个逻辑吗?另外,作为一个额外的问题,如果有人能告诉我如何使用空格而不是“·”,我会很感激;每当我打了很多次空格键并将其用引号括起来时,就像那些空格甚至不存在一样。

1 个答案:

答案 0 :(得分:1)

        int len = dr["ACCT_GRP"].Length;
        int paddingLength = 75 - len;
        string padding = new string('.', paddingLength);

我从您的问题中得知您在浏览器中查看此内容(您提到过CSS)。 HTML规范告诉浏览器将所有连续的空格折叠到一个空格中。您可以改为使用“不间断空格”字符。它可以写成“& nbs p;”在HTML中(减去s和p之间的空格)或使用其Unicode表示00 A0。所以你的c#代码变成了:

        int len = dr["ACCT_GRP"].Length;
        int paddingLength = 75 - len;
        string padding = new string('\u00A0', paddingLength);