DataGridView to CSV
作成環境 VS2010(FW2.0)
''' <summary> ''' ''' </summary> ''' <param name="sender"></param> ''' <param name="e"></param> ''' <remarks></remarks> Private Sub btnXXX_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnF11.Click Dim saveFileDialog As New SaveFileDialog() saveFileDialog.Filter = "CSVファイル(*.csv)|*.csv" If saveFileDialog.ShowDialog() <> Windows.Forms.DialogResult.OK Then Return End If Try 'ファイル書込 Using writer = New System.IO.StreamWriter(saveFileDialog.FileName, False, Constant.FileEncodeing.AAA) 'ヘッダー Dim headerList = New List(Of String) For Each col As DataGridViewColumn In dgAAA.Columns headerList.Add(col.HeaderText) Next writer.WriteLine(String.Join(",", headerList.ToArray)) '内容 For Each row As DataGridViewRow In dgAAA.Rows Dim rowContentList = New List(Of String) For columnIndex = 0 To dgAAA.Columns.Count - 1 rowContentList.Add(StringUtil.ToString(row.Cells(columnIndex).Value)) Next writer.WriteLine(String.Join(",", rowContentList.ToArray)) Next End Using 'ファイル作成完了メッセージ Catch ex As IO.IOException '保存エラーメッセージ End Try End Sub