简体   繁体   English

如何在C#中重新排列数组中的列?

[英]How to rearrange the columns in an array in C#?

So I have an array with 4 columns. 所以我有一个包含4列的数组。 And I also have a separate list which defines each column with a string. 而且我还有一个单独的列表,该列表用字符串定义每列。 Something like the snippet below:- 类似于以下代码段:

List<string> headers = new List<string>();
headers.Add("Name");
headers.Add("Number");
headers.Add("ID");
headers.Add("License");

The array looks like this 数组看起来像这样

Max 32445 1 KPFG35
Bill 33234 2 DGWEF9
Ruth 89428 3 SFD3FG

... and so on. ... 等等。

Now, lets say someone wants that same array however with the columns arranged as ID, Name, Number, License . 现在,假设有人想要相同的数组,但其列按ID, Name, Number, License排列。 How can I manipulate the columns in the array and produce a new one to return? 如何处理数组中的列并产生新的列以返回? thank you for any help! 感谢您的任何帮助!

so in the case mentioned above, it would return 因此在上述情况下,它将返回

1 Max 32445 KPFG35
2 Bill 33234 DGWEF9
3 Ruth 89428 SFD3FG

I don't know if you have to use List or not. 我不知道您是否必须使用列表。 But here is a solution that may help you. 但是,这里有一个解决方案可能会对您有所帮助。 I suggest you to use DataTable . 我建议您使用DataTable

Basically I have create a form with datagridview and a button, 基本上我已经用datagridview和一个按钮创建了一个表单,

DataTable dt = new DataTable();

In form's load, 在表单的负载中

 private void Form1_Load(object sender, EventArgs e)
    {
        dt.Columns.Add("Name");
        dt.Columns.Add("Number");
        dt.Columns.Add("ID");
        dt.Columns.Add("License");
        string[] array = { "Max", "32445", "1", "KPFG35", "Bill", "33234", "2", "DGWEF9", "Ruth", "89428", "3", "SFD3FG" };

        for (int i = 0; i < array.Length + 1; i++)
        {

            if (i != 0 && i % 4 == 0)  // every 4th item should be split from list
            {
                string[] tempArray = new string[4]; //temp array will keep every item until 4th one.
                tempArray = array.Take(i).ToArray(); //Take until 4th item.
                array = array.Skip(i).ToArray(); //then we don't need that items so we can skip them
                dt.Rows.Add(tempArray); //Row is done.
                i = -1; //Every skip will generate a new array so it should go back to 0.

            }
        }
        dataGridView1.DataSource = dt;

    }

And there is a button to change order with SetOrdinal , 还有一个按钮可以通过SetOrdinal更改订单,

private void button1_Click(object sender, EventArgs e)
        {
            dt.Columns["ID"].SetOrdinal(0);
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = dt;
            dataGridView1.Refresh();

        }

Output, 输出,

在此处输入图片说明

After button click ID column was at 0. (the second one) 在按钮单击后,ID列为0。(第二个)

Hope helps, (Not sure if you have to use List<string> , but it might be a clue for you. 希望对您有所帮助((不确定是否必须使用List<string> ,但这可能是您的一个提示。

You are not actually shifting the columns but are rearranging them. 您实际上并不是在移动列,而是在重新排列它们。 Shifting means the columns remain in the same order but the row is rotated left or right. 移位表示列保持相同顺序,但行向左或向右旋转。 Anyway, doing it without using data structures but just using arrays would take the following form in code: 无论如何,在不使用数据结构而仅使用数组的情况下,将采用以下形式的代码:

//Assuming the array is of type string
string[,] arr <--- the array we are talking about
string[,] ta = new string[3,4]; //a temporary array

/* The columns are arranged in the order: Name    Number    ID    License
    and we want it as: ------------       ID    Name    Number    License

    So, if the order of the columns is:    1   2   3   4, 
    we now want it as:  --------------     3   1   2   4  */

string order = "3124";
for(int i=0; i<3; i++){
    for(int j=0; j<4; j++){
        int k = int.Parse(order[j].ToString())-1;
                //k stores the column number in arr to be added to ta
        ta[i,j] = arr[i,k];
    }
}

//ta now stores arr in the new order
//you can either change the value of arr to the new array: arr = ta;
//or you can now make all your references to the new array ta

Hope it helps you. 希望对您有帮助。

As others have suggested: It would probably be better for you to store these as objects for maintainability. 正如其他人所建议的那样:将它们存储为可维护性对象可能会更好。

But It sounds like you have a requirement that the array is what you want to keep. 但是,听起来您有一个要保留数组的要求。 Since all you really want to do is move a column over you and produce a new list you could do something like: 由于您真正想做的就是将一列移到您的上方并生成一个新列表,因此您可以执行以下操作:

private static IEnumerable<string> MoveIndexToFirst(List<string> input)
{
    for(int i = 0; i < input.Count; i+=4 )
    {
        yield return input[i+2];
        yield return input[i];
        yield return input[i+1];
        yield return input[i+3];
    }
}

usage: 用法:

List<string> newList = MoveIndexToFirst(yourData).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM