0

私の投稿は、Visual Basic Windows Forms プログラムに関連しています。ダイアログに表示される列と非表示の列が混在するデータグリッドビュー(Device_LabelsDataGridViewと呼ばれる)があります。

「場所」(非表示) と「データ」(表示) というタイトルの特定の 2 つの列をエクスポートしたいのですが、「説明」と呼ばれる中央の列を見逃しています。このように(カンマはそのまま):

場所、説明、データ
場所、説明、データ
場所、説明、データ
続き…

しかし、私は望んでいます(繰り返しますが、コンマはそのままです)

場所、データ
場所、データ
場所、データ
続き…

以下は、使用しようとしたコードですが、必要なものを取得できません。

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の初心者であり、経験がありません.また、目に見える列と見えない列のエクスポートを組み込んだ関連する例を見つけることもできません.

テキストファイルも必要なので、他の形式は使用できません。私が作成しているプログラムは、EEPROM プログラマーです。このテキスト エクスポートが機能したら、プログラムの次の半分に取り掛かることができます。ファイルを EEPROM ハンドラに送信します。

4

1 に答える 1

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 に 3 つの列を追加し、2 番目の Visible=False を作成し、オプションをオフにして行を追加できるようにしました (DGV の右上にある小さな > をクリックします)。

列が表示されているかどうかは関係ありません。

列インデックスをテストして、列を含める必要があるかどうかを確認します。列またはその他のプロパティの HeaderText をテストできます。

.Cell.Value.Tostring は、空のセルを問題なく処理します。

于 2013-05-06T20:16:07.957 に答える