2013-05-06 49 views
0

我的帖子与Visual Basic Windows Forms程序有关。我有一个datagridview(称为:Device_LabelsDataGridView),它有一个可见和不可见的列放在一个对话框中。导出特定的DataGridView隐藏/可见列为TXT(不是整个DataGridView)

我想输出标题为“位置”(隐藏)和“数据”(可见)的特定两列,但错过了名为“说明”的中间列。出口下降我的数据是这样的(用逗号,就像它们是):

 
Location,Description,Data 
Location,Description,Data 
Location,Description,Data 
Continued… 

但我想(再次,只因为他们是逗号)

 
Location,Data 
Location,Data 
Location,Data 
Continued… 

下面是我的代码试图使用,但不能得到我想要的。

Dim writer As StreamWriter = New StreamWriter("C:\GridExport.txt") 
    If (DeviceLabels.Device_LabelsDataGridView.Rows.Count > 0) Then 
     For Each col As DataGridViewColumn In DeviceLabels.Device_LabelsDataGridView.Columns 
     Next 
    End If 

    For Each row As DataGridViewRow In DeviceLabels.Device_LabelsDataGridView.Rows 
     'If Not omitIndices.Contains(row.Index) Then 
     For Each cell As DataGridViewCell In row.Cells 
      If (cell.OwningColumn.Index = (DeviceLabels.Device_LabelsDataGridView.Columns.Count - 1)) Then 
       If (Not (cell.Value) Is Nothing) Then 
        writer.WriteLine(cell.Value.ToString) 
       Else 
        writer.WriteLine("") 
       End If 

      ElseIf (Not (cell.Value) Is Nothing) Then 
       writer.Write(String.Concat(cell.Value.ToString, ",")) 
      Else 
       writer.Write(String.Concat("", ",")) 
      End If 
     Next 
     'End If 
    Next 

    writer.Close() 

End Sub 

有人可以帮忙吗?我没有太多的头发了!我是VB的新手,有0经验,我也找不到任何相关示例,包括导出可见列和不可见列。

我也需要文本文件,所以不能使用其他格式。我正在创建的程序是一个EEPROM程序员。一旦这个文本导出工作,我可以在我的程序的下半部分工作 - 将文件发送到我的EEPROM处理程序。

回答

0

我发现使用旧的X,Y访问DGV是很容易的代码,如果你只是寻找值:

这里是我的测试案例:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 
    Dim sLine As String = "" 
    Dim sLines As String = "" ' added 
    For iRow As Integer = 0 To Device_LabelsDataGridView.RowCount - 1 
     sLine = "" 
     For iCol As Integer = 0 To Device_LabelsDataGridView.ColumnCount - 1 
      ' test header text: Device_LabelsDataGridView.Columns(iCol).HeaderText 
      If iCol = 0 Or iCol = 1 Then 
       sLine &= Device_LabelsDataGridView(iCol, iRow).Value.ToString & "," 
      End If 
     Next 
     sLines &= sLine.Substring(0, sLine.Length - 1) & vbCrLf 
    Next 
    My.Computer.FileSystem.WriteAllText("C:\Test.txt", sLines, True) ' generated from right click, snippets 
End Sub 
Private Sub Form5_Load(sender As Object, e As System.EventArgs) Handles Me.Load 
    Device_LabelsDataGridView.Rows.Add({"1a", "1b", "1c"}) 
    Device_LabelsDataGridView.Rows.Add({"2a", "2b", "2c"}) 
    Device_LabelsDataGridView.Rows.Add({"3a", "", ""}) 
    Device_LabelsDataGridView.Rows.Add({"4a", "4b", "4c"}) 
End Sub 

我加了三列到DGV并将第二个Visible设置为False,然后关闭可以添加行的选项(单击DGV右上角的小>)。

如果该列可见,则不会产生任何影响或不。

我只是测试列索引,看看我是否应该包括列或不。您可以测试HeaderText的列或其他属性。

.Cell.Value.Tostring处理空单元格确定。

+0

就像我说我是一个初学者..我不知道你的代码甚至发布文本文件,或者它将保存它。那么这是用新列编辑我的datagridview吗? – user2355434 2013-05-06 22:36:03

+0

对不起 - 我刚刚提供了部分解决方案的概述。很难发布一个完整的应用程序....我编辑了上述积累的行,然后将他们对一个文件。 – rheitzman 2013-05-07 19:35:35

+0

你可以忽略我的DataGridView加载并使用你的DataGridView。删除对网格引用的DeviceLabels前缀。修改要在输出中包含哪些列的测试。 – rheitzman 2013-05-07 19:39:42

相关问题