VB.NET DataGridView 선택 셀 색깔 색상 변경하기
2019. 3. 28. 14:03ㆍVB.NET
728x90
반응형
DataGridView를 사용하다보면 스타일을 바꾸고 싶을 때가 굉장히 많다.
특히나 고객들은 회색에 파란색으로 된 단순한 디자인보다는
화려한 스타일을 원할때가 많다.
이번에는 DataGridView에서 선택 된 셀의 색상을 바꾸었다.
선택 했을 때! 파란색으로 나오던 걸 다른 색상으로 변경하고
다시 다른 셀을 선택했을 때 ! 원래 색상으로 변경하고
이런식으로 작업이 된다.
첫 단계 : DataGridView 의 스타일 초기 지정
Public lc As ReadExcel = New ReadExcel()
Public Sub New()
InitializeComponent()
Upload_Panel.AllowDrop = True
Label1.AllowDrop = True
lc = New ReadExcel()
With DataGridView1
' 선택 했을 때 back color
.DefaultCellStyle.SelectionBackColor = Color.White
' 선택 했을 때 글씨 color
.DefaultCellStyle.SelectionForeColor = Color.Black
' Column Size를 Autosize 하지 않도록
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None
' 유저가 맘대로 바꾸고 싶을 때
.AllowUserToOrderColumns = True
' 열의 색상 지정
.ColumnHeadersDefaultCellStyle.BackColor = Color.Yellow
'열과 행의 경계선 스타일 지정
.EnableHeadersVisualStyles = False
.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single
.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single
'열에 보여지는 문자열을 여러행으로 보여주고 싶을 때
.DefaultCellStyle.WrapMode = DataGridViewTriState.True
.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
'.CellBorderStyle = DataGridViewCellBorderStyle.Sunken
End With
End Sub
두 번째 : DataGridView에 Source를 넣기 위한 코드
나 개인의 메모용도도 있기 때문에 필요한... 부분만 챙겨가시길..
Private dt_tables As DataTable
Public Property _dt_tables As DataTable
Get
Return dt_tables
End Get
Set(value As DataTable)
dt_tables = value
End Set
End Property
Private bindingSource As BindingSource
Public Property _source1 As BindingSource
Get
Return bindingSource
End Get
Set(value As BindingSource)
bindingSource = value
End Set
End Property
Private Sub DragOver_OnPanel(sender As Object, e As Windows.Forms.DragEventArgs) Handles Upload_Panel.DragEnter
If (e.Data.GetDataPresent(Windows.DataFormats.FileDrop)) Then
e.Effect = Windows.DragDropEffects.Copy
Else
e.Effect = Windows.DragDropEffects.None
End If
End Sub
Private Sub DragEnter_OnPanel(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles Upload_Panel.DragDrop
'# Array 형식으로 저장 된 것을 난 파일 하나만 허용 할 거기 때문에
Dim files() As String = e.Data.GetData(Windows.Forms.DataFormats.FileDrop)
Dim path As String = Nothing
Select Case files.Length
Case 1
' 파일이름에 스케쥴이라는 이름이 포함 된 파일만
If IO.Path.GetFileNameWithoutExtension(files(0)).Contains("스케쥴") Then
path = files(0)
Else
path = Nothing
End If
Case Else
Exit Sub
End Select
' path에 값이 없으면 팝업 띄워줌
If (path Is Nothing) Then
' Debugging은 따로 만든 메서드 임
Debugging.Show("[Error!] 올바른 파일을 올려주세요.", "lee.sunbae", 0, 16)
Exit Sub
End If
Dim tempSource As BindingSource = New BindingSource
'lc._dt_tables = lc.readAllxlsm_set_datatable(path, "Sheet2")
lc._dt_tables = lc.readAllxlsm_set_datatable(path, "Sheet11")
If Not (lc._dt_tables Is Nothing) Then
With DataGridView1
_source1 = tempSource
_source1.DataSource = lc._dt_tables
.DataSource = _source1
End With
Else
Debugging.Show("DataTable is Nothing!", "lee.sunbae", 0, 16)
End If
End Sub
세번 째 : 가장 중요한 코드 직접 Drawing 하는 부분
Private Sub myGrid_RowPostPaint(ByVal sender As Object,
ByVal e As DataGridViewRowPostPaintEventArgs) Handles DataGridView1.RowPostPaint
If e.RowIndex = DataGridView1.CurrentCell.RowIndex Then
' Calculate the bounds of the row
Dim rowHeaderWidth As Integer =
If(DataGridView1.RowHeadersVisible,
DataGridView1.RowHeadersWidth, 0)
Dim rowBounds As New Rectangle(
rowHeaderWidth,
e.RowBounds.Top,
DataGridView1.Columns.GetColumnsWidth(
DataGridViewElementStates.Visible) -
DataGridView1.HorizontalScrollingOffset + 1,
e.RowBounds.Height)
' Paint the border
ControlPaint.DrawBorder(e.Graphics, rowBounds,
Color.Red, ButtonBorderStyle.Solid)
' Paint the background color
DataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor =
Color.BlanchedAlmond
Else
' 선택 해제 했을 때
Dim rowHeaderWidth As Integer = If(DataGridView1.RowHeadersVisible, DataGridView1.RowHeadersWidth, 0)
Dim rowBounds As New Rectangle(rowHeaderWidth, e.RowBounds.Top, DataGridView1.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) -
DataGridView1.HorizontalScrollingOffset + 1, e.RowBounds.Height)
' Paint the border
ControlPaint.DrawBorder(e.Graphics, rowBounds, Color.DarkGray, ButtonBorderStyle.Solid)
' Paint the background color
DataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.White
End If
End Sub
728x90
반응형
'VB.NET' 카테고리의 다른 글
VB.NET Ctrl + A 키 입력 받기 (0) | 2019.08.20 |
---|---|
VB.NET/C# DataGridView 특정 로우 중간에 행 삽입하기 (0) | 2019.08.20 |
VB.NET 드래그 앤 드랍 구현 하기 (0) | 2019.06.20 |
VB.NET 기초 사용법 정리 (0) | 2019.06.13 |
VB.NET 여러 개의 컨트롤을 List<T>에 담아 한 번에 특정 조건의 개수 Count 하기 (0) | 2019.05.17 |