Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have dataGridView1 with two columns filled by data when click button1, data will be saved in a file, my code has many columns, how can I show only eight columns.

[Added from solution#2]

Input file:
23,56
24,67
34,77
45,79
55,66
57,67
71,43
89,45
12,46
23,66

Output file should look like this:
23,56 24,67 34,77 45,79
55,66 57,67 71,43 89,45
12,46 23,66 


What I have tried:

private void button1_Click(object sender, EventArgs e)
        {
            string filePath = "grd.txt";
            using (StreamWriter sw = new StreamWriter(filePath, append: true))
            {
                StringBuilder result = new StringBuilder();
                sw.WriteLine($" {textBox1.Text}\t{dataGridView1.Rows.Count - 1}");
                // Iterate through each row in the DataGridView
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    // Iterate through each pair of columns (i.e., merge two columns into      eight columns)
                    for (int i = 0; i < dataGridView1.Columns.Count - 1; i += 2)
                    {
                        // Check if both columns have non-null values
                        if (row.Cells[i].Value != null && row.Cells[i + 1].Value != null)
                        {
                            // Combine the values from the pair of columns separated by a comma
                            string mergedValue = string.Format("{0},{1}",
                                row.Cells[i].Value.ToString(),
                                row.Cells[i + 1].Value.ToString());

                            // Append the merged value to the result string
                            result.Append(mergedValue + " ");
                        }
                    }
                }// Save the final result to file
                sw.WriteLine(result.ToString());
                
                
            }
        }
Posted
Updated 15-Nov-23 4:44am
v2

1 solution

Have you tried to define columns?
C#
//define columns you want to export to text file
int[] colsToPrint = new int[]{1, 3, 5, 7, 9};
foreach (DataGridViewRow row in dataGridView1.Rows)
{
        // Iterate through defined columns
        foreach (int i in colsToPrint)
        {
             //your logic here
        }
}


[EDIT]
Accordingly to the solution#2 (added to the question)...
Imagine, you've got one-dimensional array and you want to "convert" it into multi-dimensional array.
Take a look at below example and read all coments carefully.

C#
string inputfile = @"D:\inputgrd.txt";
string outputfile = @"D:\outputgrd.txt";
//grab all lines into one-dimensional array
string[] items = File.ReadAllLines(inputfile);
//get the size of array
int j = items.Length;
//define new column count
int cc =  4;
//temporary variable needed to calculate rows count
int rr = j % cc;
//new rows count
int rc =  (int)(j / cc) + (rr==0 ? 0 : 1);  //use temporary variable
//define new array
string[,] a = new string[rc,cc];
//loop through the collection of items (items in array)
for(int i = 0; i < j; i++)
{
    int r = (int)i / 4;  //get row number
    int c = (int)i % 4;  //get column number
    //Console.WriteLine($"{i} => [{r},{c}]");
    //put item into proper cell of array
    a[r,c] = items[i];
}

//create an output file
StringBuilder sb = new StringBuilder();
for(int r = 0; r< rc; r++)
{
    //create new string
    string s = $"{a[r,0]} {a[r,1]} {a[r,2]} {a[r,3]}";
    Console.WriteLine(s);
    sb.AppendLine(s);
}
File.WriteAllText(outputfile, sb.ToString());


Note: It's just an example. You have to change it to your needs.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900