Passing the length and the value on a string format

Viewed 45

I have the below code that i m reading from datagrid and i export to ascii file and work fine

FileInfo info = new FileInfo(@"C:\SqlExports\");
            info.Directory.Create();

            string fileName = condition + " " + date + ".txt";

            FileStream stream = new FileStream(@"C:\SqlExports\" + fileName, FileMode.OpenOrCreate);
            StreamWriter sw = new StreamWriter(stream, Encoding.Default);

for (int i = 0; i < Grid.Rows.Count; i++)
            {
                List<string> rowprint = new List<string>();
                for (int f = 0; f < Grid.Columns.Count; f++)
                {
                    if (Grid.Rows[i].Cells[f].Value != DBNull.Value)
                    { rowprint.Add(Grid.Rows[i].Cells[f].Value.ToString()); }
                    else
                    {
                        rowprint.Add(" ");
                    }
                }
                sw.Write(string.Format("{0,-19}", rowprint[0]) + String.Format("{0,10}", rowprint[1]) + sw.NewLine, Encoding.Default);

            }
            sw.Close();

I need to make my code reusable so if the grid populated with other datas to read each time what i need.

i tried

sw.Write(string.Format("0, -(rowprint[0].length + 2)", rowprint[0]) + string.Format("{"0,rowprint[1].Length + 2"} + sw.Newlinn, Encoding.Default);

but of course not working so how can i have the length plus 2 blanks and the value?

of course i know that i need a var with the columns because its time maybe the columns is 2 or 3 or etc.

0 Answers
Related