troubleshooting Question

C# and proper way to write a fixed width format using a dataset.

Avatar of rwheeler23
rwheeler23Flag for United States of America asked on
C#
3 Comments1 Solution62 ViewsLast Modified:
I need to direct output to a text in a fixed width format.  The data is sitting in a dataset which i convert to a datatable. The problem I have is that I want columns to position that are a multiple of 40. I thought this line would do it:

                                line = line + row[column].ToString().PadRight(40 - row[column].ToString().Length, ' ');

However, what i have discovered is that     row[column].ToString().Length, ' '); will strip away the blanks at the end when it computes the length. Is there a directive that will not do that? What is the proper way to accomplish this?

                using (StreamWriter outfile = new System.IO.StreamWriter(@"C:\output.txt"))
                foreach (DataTable table in DataSet.Tables)
                {
                    foreach (DataRow row in table.Rows)                      
                    {
                        Model.line = "";
                        foreach (DataColumn column in table.Columns)
                        {
                                line = line + row[column].ToString().PadRight(40 - row[column].ToString().Length, ' ');
                        }
                            outfile.WriteLine(Model.line);
                    }
                }
ASKER CERTIFIED SOLUTION
ste5an
Senior Developer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros