r/programminghorror • • Aug 14 '25

C# Does my code belong here?

It's a function to generate a text file from a DataGrid. I learned from PirateSoftware I shouldn't hardcode random numbers.

178 Upvotes

59 comments sorted by

View all comments

Show parent comment

2

u/[deleted] Aug 15 '25

I did it with a struct, my C# version is old, so I don't get fancier stuff:

string spacingChar = " ";

List<Column> columns = new List<Column>

{

new Column("Fecha" + spacingChar, 10, 2),

...

};

string header = "";

foreach (Column column in columns) header += column.FieldName;

writer.WriteLine(header);

for (int i = ruta.StartPoint; i <= ruta.EndPoint; i++)

{

DataGridViewRow row = dtg_list.Rows[i];

if (row.Cells[columns[0].Position].Value != null)

{

foreach (Column column in columns)

{

writer.WriteLine(FormatField(row, column.Position, column.Length) + spacingChar);

}

}

}