|
|||||
�����̃T���v���̊T�v�� DataGridView�̔h���N���X������ĕ֗��ȃ��\�b�h��lj����čs���ƍ��@�\��DataGridView���o��������܂��B .NET�Ŏg�p����t���b�N�X�O���b�h(FlexGrid)�̑�\�I�ȑ��݁H�ł���DataGridView�Ȃ̂ŁA���������֗̕��� ���\�b�h��lj����ă��C�o���ɍ���t���܂��傤�B�i�ƌ�����DataGridView�̕W���Ŏ������ė~�����̂ł����j ���̃T���v����DataGridView�̎g�p���@�̃T���v���Ƃ��Ă����g�p������Ǝv���܂��B ���̃T���v���͈ȉ��̋@�\���������Ă��܂��B (�P)DataGridView�̃J���������t�@�C���֕ۑ��^�Ǎ����邽�߂̎��� (�Q)DataGridView�̊e�J�����ւ̓��͉\�����𐧌����邽�߂̎��� (�R)DataGridView��Ctrl-V�L�[�������ɃN���b�v�{�[�h����\��t���邽�߂̎��� (�S)DataGridView��Del��Backspace�L�[�������ɃZ���̓��e���������邽�߂̎��� ����ȉ��̋@�\�̎�����\�肵�Ă��܂��B (�P)DataGridView�̃R���{�{�b�N�X(ComboBox)���v���_�E��(F4)���邽�߂̎��� (�Q)DataGridView�̃Z���̓��e��CSV�`���ŕۑ����邽�߂̎��� (�R)DataGridView�̃Z���̓��e��HTML�`���ŕۑ����邽�߂̎��� (�S)DataGridView�̃Z���̓��e��EXCEL�`���ŕۑ����邽�߂̎��� (�T)DataGridView�̃Z���̓��e��DB(ACCESS��)�ɕۑ����邽�߂̎���(DB�T���v���Ƃ̑g�ݍ��킹) (�U)DataGridView�̃Z���̓��e��DB(ACCESS��)����Ǎ��ނ��߂̎���(DB�T���v���Ƃ̑g�ݍ��킹) ���Ȃ݂ɁA����DataGridViewPlus�������ƕ֗��ɂ��Ă���T�C�g���Љ�܂��B DataGridView�ŃR�s�[���y�[�X�g���\�ɂ���(�o�C���h�Ή�) DataGridView CSV�o��(C#/VB.NET) DataGridView CSV�Ǎ�(C#/VB.NET) DataGridView EXCEL�o��(C#/VB.NET) DataGridView EXCEL�Ǎ�(C#/VB.NET) DataGridView HTML�o��(C#/VB.NET) C#.NET�ł͂�����uDataGridView�ɕ֗��ȃ��\�b�h��lj�����T���v��(C#.NET)�v�ł��B �܂��ADataGridView���g�p�����ł悭�g���Z(����)�� DataGridView�̏��Z �ł��B ���̃T���v�����g�p������@(VB2005�̏ꍇ)�͈ȉ��̒ʂ�ł��B (�P)Form1��System.Windows.Forms.DataGridView��lj����� (�Q)Form1.Designer.vb��System.Windows.Forms.DataGridView�̕�����DataGridViewPlus�ɕύX����B (��)VB2010��Form1.Designer.vb�̒��̃\�[�X��\������ɂ͉��炩�̐ݒ肪�K�v�Ȃ悤�ł��B�B�B ��DataGridView����̔h���N���X(DataGridViewPlus.vb) Imports System.IO Imports System.Text Imports System.Xml.Serialization Imports System.Windows.Forms Public Class DataGridViewPlus Inherits System.Windows.Forms.DataGridView ' ---------------------------------------------------------------------------------- ' DataGridView�̃J���������t�@�C���֕ۑ��^�Ǎ����邽�߂̎��������������������������� ' ---------------------------------------------------------------------------------- ''' <summary> ''' DataGridView�̃J��������XML�`���ŃV���A���C�Y���邽�߂̃N���X ''' </summary> ''' <remarks></remarks> Public Class ColWidths ''' <summary> ''' �J������s ''' </summary> Public Widths As Integer() End Class ''' <summary> ''' DataGridView�̃J���������t�@�C���֕ۑ� ''' </summary> ''' <remarks>DataGridView�̃J��������XML�`���ŃV���A���C�Y</remarks> Public Sub SaveColWidths() Try ' EXE�t�@�C����PATH Dim ExePath As String = System.AppDomain.CurrentDomain.BaseDirectory ' XML�t�@�C����PATH Dim XmlPath As String = ExePath & "\" & Me.Parent.Name & "_" & Me.Name & ".xml" ' XML�t�@�C���I�[�v�� Dim sw As StreamWriter = New StreamWriter(XmlPath, False, Encoding.Default) Try ' �V���A���C�U�[ Dim serializer As New XmlSerializer(GetType(ColWidths)) ' DataGridView�̃J�������擾 Dim colw As New ColWidths ReDim colw.Widths(Me.Columns.Count - 1) For i As Integer = 0 To Me.Columns.Count - 1 colw.Widths(i) = Me.Columns(i).Width Next ' XML�t�@�C���ۑ� serializer.Serialize(sw, colw) Catch ex As Exception Finally ' XML�t�@�C���N���[�Y If sw Is Nothing = False Then sw.Close() End Try Catch ex As Exception End Try End Sub ''' <summary> ''' DataGridView�̃J��������O��ۑ������t�@�C������Ǎ� ''' </summary> ''' <remarks>DataGridView�̃J��������XML�`���Ńf�V���A���C�Y</remarks> Public Sub ReadColWidths() Try ' EXE�t�@�C����PATH Dim ExePath As String = System.AppDomain.CurrentDomain.BaseDirectory ' XML�t�@�C����PATH Dim XmlPath As String = ExePath & "\" & Me.Parent.Name & "_" & Me.Name & ".xml" ' XML�t�@�C���I�[�v�� Dim sr As StreamReader = New StreamReader(XmlPath, Encoding.Default) Try ' �V���A���C�U�[ Dim serializer As New XmlSerializer(GetType(ColWidths)) ' XML�t�@�C���Ǎ� Dim colw As New ColWidths colw = CType(serializer.Deserialize(sr), ColWidths) ' DataGridView�ɃJ�������ݒ� For i As Integer = 0 To Me.Columns.Count - 1 Me.Columns(i).Width = colw.Widths(i) Next Catch ex As Exception Finally ' XML�t�@�C���N���[�Y If sr Is Nothing = False Then sr.Close() End Try Catch ex As Exception End Try End Sub ' ---------------------------------------------------------------------------------- ' DataGridView�̃J���������t�@�C���֕ۑ��^�Ǎ����邽�߂̎��������������������������� ' ---------------------------------------------------------------------------------- ' ---------------------------------------------------------------------------------- ' DataGridView�̊e�J�����ւ̓��͉\�����𐧌����邽�߂̎��������������������������� ' ---------------------------------------------------------------------------------- ''' <summary> ''' �J�����ւ̓��͉\�������w�肷�邽�߂̔z�� ''' </summary> ''' <remarks>ColumnChars(0)="1234567890"</remarks> Public ColumnChars() As String ''' <summary> ''' �ҏW���̃J�����ԍ� ''' </summary> ''' <remarks></remarks> Private _editingColumn As Integer ''' <summary> ''' �ҏW����TextBoxEditingControl ''' </summary> ''' <remarks></remarks> Private _editingCtrl As DataGridViewTextBoxEditingControl ''' <summary> ''' �Z�����ҏW���ɂȂ������̏��� ''' </summary> ''' <param name="sender">�C�x���g�̔�����</param> ''' <param name="e">�C�x���g�̏��</param> ''' <remarks>�ҏW����TextBoxEditingControl��KeyPress�C�x���g�ݒ�</remarks> Private Sub DataGridViewPlus_EditingControlShowing( _ ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs) _ Handles Me.EditingControlShowing ' �ҏW���̃J�����ԍ���ۑ� _editingColumn = CType(sender, DataGridView).CurrentCellAddress.X Try ' �ҏW����TextBoxEditingControl��KeyPress�C�x���g�ݒ� _editingCtrl = CType(e.Control, DataGridViewTextBoxEditingControl) AddHandler _editingCtrl.KeyPress, AddressOf DataGridViewPlus_CellKeyPress Catch End Try End Sub ''' <summary> ''' �Z���̕ҏW���I��������̏��� ''' </summary> ''' <param name="sender">�C�x���g�̔�����</param> ''' <param name="e">�C�x���g�̏��</param> ''' <remarks>�ҏW����TextBoxEditingControl����KeyPress�C�x���g�폜</remarks> Private Sub DataGridViewPlus_CellEndEdit(ByVal sender As Object, _ ByVal e As DataGridViewCellEventArgs) Handles Me.CellEndEdit If _editingCtrl Is Nothing = False Then ' �ҏW����TextBoxEditingControl����KeyPress�C�x���g�폜 RemoveHandler _editingCtrl.KeyPress, AddressOf DataGridViewPlus_CellKeyPress _editingCtrl = Nothing End If End Sub ''' <summary> ''' �ҏW����TextBoxEditingControl��KeyPress�̏��� ''' </summary> ''' <param name="sender">�C�x���g�̔�����</param> ''' <param name="e">�C�x���g�̏��</param> ''' <remarks>�͉\�����̔���</remarks> Private Sub DataGridViewPlus_CellKeyPress( _ ByVal sender As Object, ByVal e As KeyPressEventArgs) ' �J�����ւ̓��͉\�������w�肷�邽�߂̔z�w�肳��Ă��邩�`�F�b�N If IsArray(ColumnChars) Then ' �J�����ւ̓��͉\�������w�肷�邽�߂̔z�`�F�b�N If ColumnChars.GetLength(0) - 1 >= _editingColumn Then ' �J�����ւ̓��͉\�������w�肳��Ă��邩�`�F�b�N If ColumnChars(_editingColumn) <> "" Then ' �J�����ւ̓��͉\�������`�F�b�N If InStr(ColumnChars(_editingColumn), e.KeyChar) <= 0 AndAlso _ e.KeyChar <> Chr(Keys.Back) Then ' �J�����ւ̓��͉\�����ł͖����̂Ŗ��� e.Handled = True End If End If End If End If End Sub ' ---------------------------------------------------------------------------------- ' DataGridView�̊e�J�����ւ̓��͉\�����𐧌����邽�߂̎��������������������������� ' ---------------------------------------------------------------------------------- ' ---------------------------------------------------------------------------------- ' DataGridView��Ctrl-V�L�[�������ɃN���b�v�{�[�h����\��t���邽�߂̎��������������� ' DataGridView��Del��Backspace�L�[�������ɃZ���̓��e���������邽�߂̎��������������� ' ---------------------------------------------------------------------------------- Private Sub DataGridViewPlus_KeyDown( _ ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) _ Handles Me.KeyDown Dim dgv As DataGridView = CType(sender, DataGridView) Dim x As Integer = dgv.CurrentCellAddress.X Dim y As Integer = dgv.CurrentCellAddress.Y If e.KeyCode = Keys.Delete Or e.KeyCode = Keys.Back Then ' �Z���̓��e������ dgv(x, y).Value = "" ElseIf (e.Modifiers And Keys.Control) = Keys.Control And e.KeyCode = Keys.V Then ' �N���b�v�{�[�h�̓��e���擾 Dim clipText As String = Clipboard.GetText() ' ���s��ϊ� clipText = clipText.Replace(vbCrLf, vbLf) clipText = clipText.Replace(vbCr, vbLf) ' ���s�ŕ��� Dim lines() As String = clipText.Split(vbLf) Dim r As Integer Dim nflag As Boolean = True For r = 0 To lines.GetLength(0) - 1 ' �Ō��NULL�s���R�s�[���邩�ǂ��� If r >= lines.GetLength(0) - 1 And _ "".Equals(lines(r)) And nflag = False Then Exit For If "".Equals(lines(r)) = False Then nflag = False ' �^�u�ŕ��� Dim vals() As String = lines(r).Split(vbTab) ' �e�Z���̒l��ݒ� Dim c As Integer = 0 Dim c2 As Integer = 0 For c = 0 To vals.GetLength(0) - 1 ' �Z�������݂��Ȃ���Γ\��t���Ȃ� If Not (x + c2 >= 0 And x + c2 < dgv.ColumnCount And _ y + r >= 0 And y + r < dgv.RowCount) Then Continue For End If ' ��\���Z���ɂ͓\��t���Ȃ� If dgv(x + c2, y + r).Visible = False Then c = c - 1 Continue For End If '' �\��t������(���͉\�����`�F�b�N�����̎�)------------ '' �s�lj����[�h&(�ŏI�s�̎��͍s�lj�) 'If y + r = dgv.RowCount - 1 And _ ' dgv.AllowUserToAddRows = True Then ' dgv.RowCount = dgv.RowCount + 1 'End If '' �\��t�� 'dgv(x + c2, y + r).Value = vals(c) ' ------------------------------------------------------ ' �\��t������(���͉\�����`�F�b�N�L��̎�)------------ Dim pststr As String = "" For i As Long = 0 To vals(c).Length - 1 _editingColumn = x + c2 Dim tmpe As KeyPressEventArgs = _ New KeyPressEventArgs(vals(c).Substring(i, 1)) tmpe.Handled = False DataGridViewPlus_CellKeyPress(sender, tmpe) If tmpe.Handled = False Then pststr = pststr & vals(c).Substring(i, 1) End If Next ' �s�lj����[�h���ŏI�s�̎��͍s�lj� If y + r = dgv.RowCount - 1 And _ dgv.AllowUserToAddRows = True Then dgv.RowCount = dgv.RowCount + 1 End If ' �\��t�� dgv(x + c2, y + r).Value = pststr ' ------------------------------------------------------ ' ���̃Z���� c2 = c2 + 1 Next Next End If End Sub ' ---------------------------------------------------------------------------------- ' DataGridView��Ctrl-V�L�[�������ɃN���b�v�{�[�h����\��t���邽�߂̎��������������� ' DataGridView��Del��Backspace�L�[�������ɃZ���̓��e���������邽�߂̎��������������� ' ---------------------------------------------------------------------------------- End Class ���e�X�g�p�t�H�[��(Form1.vb) ' �\��From1��System.Windows.Forms.DataGridView��lj����Ă��������B ' ������Form1.Designer.vb��System.Windows.Forms.DataGridView�̕����� ' DataGridViewPlus�ɕύX���Ă��������B Public Class Form1 Private Sub Form1_Load( _ ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load ' �Z���I�����[�h DataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect ' �s�w�b�_�[���� DataGridView1.RowHeadersVisible = False ' �J�����w�b�_�[�͒����\�� DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = _ DataGridViewContentAlignment.MiddleCenter ' �J�����̒lj� ' TextBox�J�����̒lj� DataGridView1.Columns.Add("aaa", "AAA") DataGridView1.Columns.Add("bbb", "BBB") 'DataGridView1.Columns.Add("ccc", "CCC") ' ComboBox�J�����̒lj� Dim cbo As DataGridViewComboBoxColumn = New DataGridViewComboBoxColumn() cbo.HeaderText = "CCC" cbo.Name = "ccc" Dim i As Integer For i = 1 To 30 cbo.Items.Add("" & i) Next DataGridView1.Columns.Add(cbo) ' �e�J������ҏW�\�ɐݒ� For i = 0 To DataGridView1.Columns.Count - 1 DataGridView1.Columns(i).[ReadOnly] = False Next ' �s�̒lj� DataGridView1.Rows.Clear() DataGridView1.Rows.Add() DataGridView1.Rows(0).Cells(0).Value = "11" DataGridView1.Rows(0).Cells(1).Value = "12" DataGridView1.Rows(0).Cells(2).Value = "13" DataGridView1.Rows.Add() DataGridView1.Rows(1).Cells(0).Value = "21" DataGridView1.Rows(1).Cells(1).Value = "22" DataGridView1.Rows(1).Cells(2).Value = "23" ' DataGridView�̃J�������Ǎ� DataGridView1.ReadColWidths() ' DataGridView�̓��͉\�����w�� Dim ColumnChars() As String ReDim ColumnChars(DataGridView1.Columns.Count - 1) ColumnChars(0) = "abc" ColumnChars(1) = "123" ColumnChars(2) = "" DataGridView1.ColumnChars = ColumnChars End Sub Private Sub Form1_FormClosed( _ ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) _ Handles Me.FormClosed ' DataGridView�̃J�������ۑ� DataGridView1.SaveColWidths() End Sub End Class �����Ȃ݂ɁE�E�EDataGridView���g�p�����ł悭�g���Z �EDataGridView�̍s�lj����@ DataGridView1.Rows.Add() �EDataGridView�̍s�폜���@ DataGridView1.Rows.RemoveAt(0) �EDataGridView�̃J�����폜���@(�J�����w�b�_���폜����܂�) DataGridView1.Columns.RemoveAt(0) �EDataGridView�̑S�s�폜���@(���ӁF�J�������̎������[�h�ŗ�O����������\���L��) DataGridView.Rows.Clear() �EDataGridView�̎w��J�����Ń\�[�g������@ DataGridView1.Sort(DataGridView1.Columns(0), ListSortDirection.Ascending) �EDataGridView�̍s��J��������͋֎~(ReadOnly)�ɂ�����@ DataGridView1.Rows(0).ReadOnly = True DataGridView1.Columns(0).ReadOnly = True �EDataGridView�̎w��Z���̔w�i�F��ύX������@ DataGridView1.Rows(i).Cells("clmName").Style.BackColor = Color.LightSkyBlue �EDataGridView�ɕ\������擪�s(�X�N���[�����̐擪�s)��ύX������@ DataGridView1.FirstDisplayedScrollingRowIndex = 10 �EDataGridView�̃X�N���[���͈͂ŃZ�����\������Ă��邩�m�F������@ If DataGridView1.Rows(4).Cells("clmName").Displayed Then �EDataGridView�̃J�����g�Z�����擾�^�ύX������@ Dim v as String = DataGridView1.CurrentCell.Value DataGridView1.CurrentCell = DataGridView1.Rows(4).Cells("clmName") �EDataGridView�̃J�����g�Z���̃t�H�[�J�X���������@ Private Sub DataGridView1_SelectionChanged( _ ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles DataGridView1.SelectionChanged Dim dgv As DataGridView = sender dgv.CurrentCell = Nothing End Sub |