简体   繁体   English

Datagridview不显示标签字符

[英]Datagridview not displaying tab character

I use a visual basic program to get information from another program, which we'll call ProgramX. 我使用一个可视的基本程序从另一个程序(我们称为ProgramX)获取信息。

ProgramX has built in functionality to generate tab-delimited tables, which many users copy over to Excel. ProgramX具有内置功能来生成制表符分隔的表,许多用户将其复制到Excel。 My goal(which I've acheived) is to generate and collect upwards of 1,000 of these tables and have the results ready for the user to copy/paste into Excel all at once instead of one at a time. 我的目标(我已经实现)是生成并收集多达1,000个这些表,并使结果准备好供用户一次性复制/粘贴到Excel中,而不是一次复制/粘贴到Excel中。 My Dataset keeps the tab-delimited records just as I would like them. 我的数据集将保留制表符分隔的记录,就像我想要的一样。

The issue is that when I display my dataset/datatable in my datagridview, all tab characters are removed. 问题是,当我在datagridview中显示数据集/数据表时,所有选项卡字符都将被删除。 When the results are copied/pasted into Excel they take up one column instead of automatically breaking out. 将结果复制/粘贴到Excel中后,它们占据一列,而不是自动拆分。 I have been using datagridview to preview results/copy to clipboard; 我一直在使用datagridview预览结果/复制到剪贴板; Is there any way to retain the tabs in this view? 有什么办法可以在此视图中保留选项卡?

Expected Result: Result 1 [tab] Result 2 [tab] Result 3 预期结果:结果1 [选项卡]结果2 [选项卡]结果3

Result in datatable: Result 1 [tab] Result 2 [tab] Result 3 数据表中的结果:结果1 [选项卡]结果2 [选项卡]结果3

Result in datagridview: Result 1Result 2Result 3 datagridview中的结果:结果1结果2结果3

If the only way to achieve this is to copy directly from my datatable, I have seen a few posts on how to accomplish that. 如果要实现这一目标的唯一方法是直接从我的数据表中复制,我已经看到了一些有关如何实现此目标的文章。 Thanks! 谢谢!

You have numerous options available, but in most cases you'll need to perform some looping one way or another - however even with 1000+ records it's performance hardly takes a hit. 您有许多可用的选项,但是在大多数情况下,您需要以一种或另一种方式执行一些循环-但是,即使有1000多个记录,其性能也几乎不会受到影响。

So for the first couple of options let's assume I have the following setup - keeping with your one DataGridViewColumn idea: 因此,对于前几个选项,假设我具有以下设置-遵循您的一个DataGridViewColumn想法:

Me.table = New DataTable()
Me.table.Columns.Add("Data", GetType(String))

For i As Integer = 0 To 999
    Dim x As Integer = i * 4
    Dim data As String = String.Format("Result {0}" & vbTab & "Result {1}" & vbTab & "Result {2}" & vbTab & "Result {3}", x + 1, x + 2, x + 3, x + 4)
    Me.table.Rows.Add(data)
Next

Me.dataGridView1.AllowUserToAddRows = False
Me.dataGridView1.DataSource = Me.table

Possible Solutions 可能的解决方案

  1. Loop through the DataTable , concatenate the data, and set the clipboard text: 遍历DataTable ,连接数据,并设置剪贴板文本:

     Private Sub button1_Click(sender As Object, e As System.EventArgs) Handles button1.Click Dim content As String = String.Empty For Each row As DataRow In Me.table.Rows content = String.Format("{0}" & vbLf & "{1}", content, row.ItemArray(0).ToString()) Next content = content.TrimStart(ControlChars.Lf) Clipboard.SetText(content) End Sub 
  2. Loop through the DataGridView.Rows , concatenate the data, and set the clipboard text: 遍历DataGridView.Rows ,连接数据,并设置剪贴板文本:

     Private Sub button1_Click(sender As Object, e As System.EventArgs) Handles button1.Click Dim content As String = String.Empty For Each row As DataGridViewRow In Me.dataGridView1.Rows content = String.Format("{0}" & vbLf & "{1}", content, row.Cells(0).Value.ToString()) Next content = content.TrimStart(ControlChars.Lf) Clipboard.SetText(content) End Sub 

  • And to make the grid look a little better for both option 1 and 2, since the display seems to ignore tabs: 为了使选项1和2的网格看起来更好一点,因为显示似乎忽略了选项卡:

     Private Sub dataGridView1_CellFormatting(ByVal sender As Object, _ ByVal e As DataGridViewCellFormattingEventArgs) _ Handles dataGridView1.CellFormatting Dim value As String = e.Value.ToString().Replace(vbTab, " ") e.Value = value End Sub 

    With that your grid might look like: 这样,您的网格可能看起来像:

    dgv一栏


  1. In addition to the original setup and instead of binding to the original DataTable , split the table's data by the tabs and display them in separate columns: 除了原始设置之外,不绑定到原始DataTable ,而是通过选项卡拆分表的数据,并将其显示在单独的列中:

     Dim splitTable As New DataTable() For Each row As DataRow In Me.table.Rows Dim splitItems = row.ItemArray(0).ToString().Split(ControlChars.Tab) For i As Integer = splitTable.Columns.Count To splitItems.Length - 1 splitTable.Columns.Add(String.Empty, GetType(String)) Next splitTable.Rows.Add(splitItems) Next Me.dataGridView1.AllowUserToAddRows = False Me.dataGridView1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText Me.dataGridView1.DataSource = splitTable 

    Then you can use the built-in clipboard method for the DataGridView : 然后,您可以对DataGridView使用内置的剪贴板方法:

     Private Sub button1_Click(sender As Object, e As System.EventArgs) Handles button1.Click Me.dataGridView1.SelectAll() Clipboard.SetDataObject(Me.dataGridView1.GetClipboardContent()) End Sub 

    With that your grid might look like: 这样,您的网格可能看起来像:

    具有多列的dgv


For all three options, clicking button1 to copy the data and hitting Ctrl + V in Excel will produce: 对于这三个选项,单击button1复制数据并在Excel中按Ctrl + V将产生:

出色的结果

Note that Wrap Text will be on by default . 请注意, 默认情况下 ,“ 环绕文字”将处于启用状态


Pros: 优点:

Options 1 and 2 leave the user-selected cells intact. 选项1和2保留用户选择的单元格不变。 Option 3 looks better with separated DataGridView columns. 选项3与单独的DataGridView列一起看起来更好。

Cons: 缺点:

Options 1 and 2 can look less clean pending the data. 选项1和2在等待数据时看起来不太干净。 Option 3 selects all cells, losing the user's previous selected cells. 选项3选择所有单元格,丢失用户先前选择的单元格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM