본문 바로가기
VB

[VB.NET] DataGridView

by 엘딘 2022. 5. 12.

 

 

 

Public Class Form1
    Private Const V As Integer = 2

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect

        ' Grid 열 제목
        DataGridView1.ColumnCount = V
        DataGridView1.Columns(0).Name = "이름"
        DataGridView1.Columns(1).Name = "주소"
    End Sub

    ' 추가버튼
    Private Sub addBtn_Click(sender As Object, e As EventArgs) Handles addBtn.Click
        If nameTxt.Text = "" Or addrTxt.Text = "" Then
            MessageBox.Show("데이터를 입력하세요")
            Return
        End If

        Dim row As String() = New String() {nameTxt.Text, addrTxt.Text}

        DataGridView1.Rows.Add(row)

        nameTxt.Text = ""
        addrTxt.Text = ""
    End Sub

    ' grid에 있는 데이터를 텍스트박스에 넣어줌(수정을 위해서)
    Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        If (e.RowIndex > -1) And Not (DataGridView1.SelectedRows(0).Cells(0).Value = Nothing) Then
            nameTxt.Text = DataGridView1.SelectedRows(0).Cells(0).Value.ToString
            addrTxt.Text = DataGridView1.SelectedRows(0).Cells(1).Value.ToString
        Else
            nameTxt.Text = ""
            addrTxt.Text = ""
        End If
    End Sub

    ' 업데이트 버튼
    Private Sub updateBtn_Click(sender As Object, e As EventArgs) Handles updateBtn.Click
        DataGridView1.SelectedRows(0).Cells(0).Value = nameTxt.Text
        DataGridView1.SelectedRows(0).Cells(1).Value = addrTxt.Text
    End Sub

    ' 삭제버튼
    Private Sub deleteBtn_Click(sender As Object, e As EventArgs) Handles deleteBtn.Click
        If DataGridView1.SelectedRows(0).Cells(0).Value = Nothing Then
            Return
        End If

        Dim msgResult As MsgBoxResult = MessageBox.Show("삭제하시겠습니까?", "삭제", MessageBoxButtons.YesNo)

        If msgResult = MsgBoxResult.Yes Then
            DataGridView1.Rows.RemoveAt(DataGridView1.SelectedRows(0).Index)

            nameTxt.Text = ""
            addrTxt.Text = ""
        End If
    End Sub


End Class

 

'VB' 카테고리의 다른 글

[Vb 6.0] 체크박스 / 라디오버튼  (0) 2022.05.13
[VB.NET] GridView 데이터로 엑셀 파일 생성하기  (0) 2022.05.12
[VB.NET] 엑셀 파일 생성  (0) 2022.05.12
[VB.NET] Excel 연결  (0) 2022.05.12
[C#] 숫자를 한글로 표현  (0) 2022.05.11

댓글