首页 > 解决方案 > 格式化数组时在“string.join()”中添加新行?

问题描述

您好,我是新手程序员,我正在尝试以在特定数组集末尾有换行符的方式格式化数组。我目前有 4 个单独的数组,我想将数组中的每个项目排列成特定的模式。我已经完成了这项任务,但现在我很难过,因为它们都在一条线上。让我举个例子:(顺便说一下,我在datagridview上这样做)

(This is what I want to happen)
Binder Clips Small  pc      12         1260
Selleys All Clear   pc      12         2400
(This is what I am getting)

Binder Clips Small  pc      12          1260        Selleys All Clear      pc    12     2400 

这是我的代码:

//I get these from a datagridview
            var items = carto.Rows
            .Cast<DataGridViewRow>()
            .Select(x => x.Cells[1].Value.ToString().Trim())
            .ToArray();

            var units = carto.Rows
            .Cast<DataGridViewRow>()
            .Select(x => x.Cells[2].Value.ToString().Trim())
            .ToArray();

            var quantity = carto.Rows
            .Cast<DataGridViewRow>()
            .Select(x => x.Cells[6].Value.ToString().Trim())
            .ToArray();

            var prices = carto.Rows
            .Cast<DataGridViewRow>()
            .Select(x => x.Cells[8].Value.ToString().Trim())
            .ToArray();

            //this is what I use to sort out the pattern that I want the arrays to be in
            string[] concat = new string[items.Length * 4];
            int index = 0;
            for (int i = 0; i < items.Length; i++)
            {
                concat[index++] = items[i];
                concat[index++] = units[i];
                concat[index++] = quantity[i];
                concat[index++] = prices[i];
            }

// and this is where I am stuck because I can just put \n, it would ruin the format even more
            cartitems.Text = string.Join(" ", concat); 

我也尝试做这样的事情:

            int j = 0;
            string str = "";

            foreach (var item in concat)
            {
                str += item;
                if (j <= concat.Length - 1)
                {
                    if (j % 3 == 0)
                        str += " ";
                    else
                        str += "\n";
                }
                j++;
            }

它有点完成工作,但换行符无处不在。这就是我的项目的样子,因此您可以更好地了解我从 4 个数组中获取数据的位置: 在此处输入图像描述

基本上是产品名称、单位、数量和总行数,最后我存储在标签上,这样我就可以看到格式的样子: 在此处输入图像描述

关于总结我的问题,我真的希望你能帮助像我这样的新手,我觉得答案很简单,我只是没有经验。

标签: c#arraysdatagridviewformatting

解决方案


作为一般规则,您应该将数据结构(数据的存储方式,在此作为值数组实现)与数据表示(在这种情况下,写入列表框)分开。

C# 是一种面向对象的语言,所以我们不妨利用它,对吧?

为您的项目创建一个类。

class Item
{
    public string Name { get; set; }
    public string Unit { get; set; }
    public string Quantity { get; set; }
    public string Price { get; set; }

    override public string ToString() {
        return $"{Name} {Unit} {Quantity} {Price}";
    }
}

这就是您加载阵列的方式。

Item[] concat = new Item[items.Length];
int index = 0;
for (int i = 0; i < items.Length; i++) {
    concat[index++] = new Item { 
        Name = items[i], 
        Unit = units[i], 
        Quantity = quantity[i], 
        Price = prices[i] 
    };
}

这就是您可以将项目列表添加到列表框的方式。

foreach(Item item in concat) {
    listBox.Items.Add(item);
}

推荐阅读