Public Class fSelectWeatherGroup

    Public SelectedCode As String = String.Empty
    Dim preCode As String
    Dim bInit As Boolean
    Public Sub New(precode_ As String)

        ' 디자이너에서 이 호출이 필요합니다.
        InitializeComponent()
        Me.preCode = precode_
        ' InitializeComponent() 호출 뒤에 초기화 코드를 추가하세요.

    End Sub
    Private Sub fSelectArea_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        bInit = False
        bsArea1.DataSource = DSET1
        bsArea2.DataSource = DSET2
        bsArea1.Filter = "code like '%0000'"
        bsArea2.Filter = "code = 'test'"

        '//좌측목록에 데이터를 표시한다 
        Dim list = From m In DSET1.weather_group
                   Where m.code.EndsWith("0000")
                   Select m



        lv1.Items.Clear()
        For Each item As DS.weather_groupRow In list
            Dim lvitem As ListViewItem = lv1.Items.Add(item.name)
            lvitem.Tag = item.code

            '//입력된 코드가 있다면 선택해준다
            If preCode.Length = 6 Then
                If item.code.Substring(0, 2) = preCode.Substring(0, 2) Then
                    lvitem.Selected = True
                    lvitem.Focused = True
                End If
            End If

        Next

        '//입력된 코드가 있다면 중분류를 선택해준다
        If preCode.Length = 6 Then
            SelectGrp2Code(preCode)
        End If


        bInit = True

        '//선택되지 않았다면 1번아이템을 선택해준다
        If lv1.SelectedItems.Count < 1 AndAlso lv1.Items.Count > 0 Then
            lv1.Items(0).Selected = True    '//중분류는 이 걸로 자동 선택됨
        End If

        Me.Show()
        Me.lv1.Select()

    End Sub
    Private Sub lv1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lv1.SelectedIndexChanged
        '//왼쪽에서 목록을 선태갛면 그걸로 시작하는 데이터를 표시해줘야하낟

        If bInit = False OrElse lv1.SelectedItems.Count < 1 Then Return

        Dim GrpItem As ListViewItem = lv1.SelectedItems(0)
        Dim GrpCode As String = GrpItem.Tag.ToString()

        SelectGrp2Code(GrpCode)
        'Dim list = From m In DSET1.weather_group
        '           Where m.code.StartsWith(GrpCode.Substring(0, 2)) AndAlso m.code.EndsWith("0000") = False AndAlso m.code.EndsWith("00")
        '           Select m

        'lv2.Items.Clear()
        'For Each item As DS.weather_groupRow In list
        '    Dim lvitem As ListViewItem = lv2.Items.Add(item.name)
        '    lvitem.Tag = item.code
        'Next

    End Sub

    Sub SelectGrp2Code(GrpCode As String)

        '//해당 중분류 아이템을 가져온다
        Dim list = From m In DSET1.weather_group
                   Where m.code.StartsWith(GrpCode.Substring(0, 2)) AndAlso m.code.EndsWith("0000") = False AndAlso m.code.EndsWith("00")
                   Select m

        lv2.Items.Clear()
        For Each item As DS.weather_groupRow In list
            Dim lvitem As ListViewItem = lv2.Items.Add(item.name)
            lvitem.Tag = item.code
            If GrpCode = item.code Then
                lvitem.Focused = True
                lvitem.Selected = True
            End If
        Next
    End Sub


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btOK.Click
        If lv2.SelectedItems.Count < 1 Then
            MessageBox.Show("중분류를 선택하세요", "확인")
            Return
        End If

        '
        SelectedCode = lv2.SelectedItems(0).Tag.ToString()
        DialogResult = DialogResult.OK
    End Sub


End Class