開発環境 VisualStudio2017
OS Windows10
フレームワーク .NET Framework 4.7.2
DataGridViewをFormにドッキングさせる方法
ドッキング方法はプロパティでDataGridViewを選択すると
デザインビューでDataGridViewがアクティブになり
右上に三角マークが出るのでこれをクリック
「DataGridViewのタスク」のダイアログが出るので
下にある「親コンテナーにドッキングする」をクリック
するとメニューを除くフォームの大きさになる
命名規則とプロパティ設定値
MenuStrip mnuStrip OpenFileDialog ofdCsv ofdCsv.Filter=Csvファイル|*.csv ToolStripMenuItem CsvOpenToolStripMenuItem CsvOpenToolStripMenuItem.Text=Csvファイルを開く DataGridView dgvCsv DataSet oDS DataTable oTB DataGridViewColumn oColumn OleDb.OleDbConnection oCon OleDb.OleDbCommand() oCommand OleDb.OleDbDataAdapter oDataAdapter
VB.netコード
Public Class frmCsv Private Sub CsvOpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CsvOpenToolStripMenuItem.Click ofdCsv.ShowDialog() End Sub Private Sub ofdCsv_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles ofdCsv.FileOk Dim sHeader As String If MsgBox("ヘッダー(1行目)にタイトルを含みますか?", MsgBoxStyle.YesNo, "ヘッダーの有無指定") = MsgBoxResult.Yes Then sHeader = "YES" Else sHeader = "NO" End If Dim oCon As New OleDb.OleDbConnection Dim oCommand As New OleDb.OleDbCommand() Try Dim sFileName As String = ofdCsv.SafeFileName Dim sFilePath As String = Strings.Left(ofdCsv.FileName, Len(ofdCsv.FileName) - Len(sFileName)) Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sFilePath & ";Extended Properties='Text;HDR=" & sHeader & ";FMT=CSVDelimited';" Dim oDataSet As New DataSet Dim oDataAdapter As New OleDb.OleDbDataAdapter Dim oDataTable As DataTable = New DataTable oCon.ConnectionString = sConnectionString oCommand.Connection = oCon oCommand.CommandText = "SELECT * FROM [" & sFileName & "]" oDataAdapter.SelectCommand = oCommand oDataSet.Clear() oDataAdapter.Fill(oDataTable) dgvCsv.DataSource = oDataTable Dim oColumn As DataGridViewColumn For Each oColumn In dgvCsv.Columns oColumn.SortMode = DataGridViewColumnSortMode.NotSortable Next Catch ex As Exception Debug.Write(ex.Message) Finally If Not Command() Is Nothing Then oCommand.Dispose() If Not oCon Is Nothing Then oCon.Dispose() End Try End Sub End Class
C#コード
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.OleDb; //←追加 namespace CsvViewCSharp { public partial class frmCsv : Form { public frmCsv() { InitializeComponent(); } private void CsvOpenFileToolStripMenuItem_Click(object sender, EventArgs e) { ofdCsv.ShowDialog(); } private void ofdCsv_FileOk(object sender, CancelEventArgs e) { bool bIsHeader = MessageBox.Show("ヘッダー(1行目)にタイトルを含みますか?", "ヘッダーの有無指定", MessageBoxButtons.YesNo) == DialogResult.Yes; string sHeader = bIsHeader ? "YES" : "NO"; string sFileName = ofdCsv.SafeFileName; string sFilePath = ofdCsv.FileName.Substring(0, ofdCsv.FileName.Length - sFileName.Length); string sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sFilePath + ";Extended Properties='Text;HDR=" + sHeader + ";FMT=CSVDelimited';"; OleDbConnection oCon = new OleDbConnection(sConnectionString); OleDbCommand oCommand = new OleDbCommand("SELECT * FROM [" + sFileName + "]", oCon); OleDbDataAdapter oDataAdapter = new OleDbDataAdapter(oCommand); DataSet oDataset = new DataSet(); DataTable oDataTable = new DataTable(); oDataset.Clear(); oDataAdapter.Fill(oDataTable); dgvCsv.DataSource = oDataTable; foreach (DataGridViewColumn oColumn in dgvCsv.Columns) { oColumn.SortMode = DataGridViewColumnSortMode.NotSortable; } if (oCommand != null) { oCommand.Dispose(); } if(oCon != null) { oCon.Dispose(); } } } }