将datagrid输出文本文件对齐到列中 [英] align datagrid output text files into columns

查看:76
本文介绍了将datagrid输出文本文件对齐到列中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将数据网格的内容从数据网格导出到文本文件并将它们对齐到其适当的列...我已经能够将数据网格的内容写入文本文件.....但是我最大的问题是如何格式化文本以使它们正确对齐到数据网格视图中出现的列....请帮助...,

这是文本文件的样子:

Drawing       	0       	1596       
Capital       	1435       	0       
Sales       	0       	20607       
Purchaes       	15486       	0       
Purchases resturn       	0       	201       
Stock at 1 Jan       	2107       	0       
Carriage on purchases       	105       	0       
carriage on sales       	188       	0       
rent and rate       	460       	0 



这是我使用的代码::::

USIng objectwriter=io.file.append("c:/er.text")
For Each row As DataGridViewRow In Me.DataGridView1.Rows
                    If Not row.IsNewRow Then
                        Dim line As String = String.Empty
                        For x As Integer = 0 To row.Cells.Count - 1
           line &= row.Cells(x).Value &"     " vbTab
                        Next
                        objectwriter.WriteLine(line.Remove(line.Length - 1, 1))
                    End If
                Next
end using


请帮助:
我如何格式化文本文件以将文本内容对齐到出现在datagrid视图中的适当的行和列,谢谢!
[edit]添加了代码块-OriginalGriff [/edit]

解决方案

您要么需要找到最长的文本字符串,而且要在每一列中找到最大的数字,所以您可以在所有其他字段中留出空间,或对每个字段使用固定的宽度.
然后,您可以在WriteLine函数中使用字符串格式输出固定宽度的字段:
例如:

Console.WriteLine("{0,-40}:{1,6}", "Hello", 167)

将打印

Hello                                   :   167


am trying to export contents from datagrid to a text file and align them into their their proper columns...i have been able to write the contents of a datagrid to the textfile.....but my greatest problem is how to FORMAT the text to align them properly into their columns as it appeared on the datagrid view....please help...,

this is HOW the text file looked like :

Drawing       	0       	1596       
Capital       	1435       	0       
Sales       	0       	20607       
Purchaes       	15486       	0       
Purchases resturn       	0       	201       
Stock at 1 Jan       	2107       	0       
Carriage on purchases       	105       	0       
carriage on sales       	188       	0       
rent and rate       	460       	0 



THIS IS THE CODE I used::::

USIng objectwriter=io.file.append("c:/er.text")
For Each row As DataGridViewRow In Me.DataGridView1.Rows
                    If Not row.IsNewRow Then
                        Dim line As String = String.Empty
                        For x As Integer = 0 To row.Cells.Count - 1
           line &= row.Cells(x).Value &"     " vbTab
                        Next
                        objectwriter.WriteLine(line.Remove(line.Length - 1, 1))
                    End If
                Next
end using


PLEASE HELP:
how CAN I FORMAT the text file to align the contents of the text into proper row and columns as they appeared on the datagrid view thanks In advance!!!

[edit]Code block added - OriginalGriff[/edit]

解决方案

You either need to find the longest text string, and the biggest number in each column, so you can allow space in all the others, or use a fixed width for each field.
You can then use a string Format in your WriteLine function to output fixed width fields:
For example:

Console.WriteLine("{0,-40}:{1,6}", "Hello", 167)

Would print

Hello                                   :   167


这篇关于将datagrid输出文本文件对齐到列中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆