본문 바로가기
C#

C# WPF ComboBox 활용하기

by orange+ 2023. 5. 17.

안녕하세요. 오늘은 데이터베이스에서 읽어온 코드와 코드명을

콤보박스의 항목으로 추가하는 방법을 공유해 볼까 합니다.

 

특별한 내용은 없지만 C#을 시작하시는 분들에게는 도움이 되겠지요? ^^

 

            con = new SQLiteConnection(cs);
            string tmptxt = null;

            try
            {
                con.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            try
            {
                콤보박스.Items.Clear();

                //소득구분 기준코드 데이터 가져오기
                string sql = "SELECT code, codenm " +
                    " FROM BaseCode " ;

                cmd = new SQLiteCommand(sql, con);
                DataTable dt = new DataTable();

                dr = cmd.ExecuteReader();
                dt.Load(dr);            

                foreach (DataRow row in dt.Rows)
                {
                    ComboBoxItem item = new ComboBoxItem();
                    item.Content = row[1].ToString();
                    item.Tag = row[0].ToString();

                    콤보박스.Items.Add(item);
                }
                dr.Close();
                con.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }

 

 

 

 

행복한 하루 되세요.

감사합니다.

 

 

 

그런데 혹시 가슴이 답답해 명상이나 힐링이 필요하시다면?

들러 주세요~ ^^

 

https://youtu.be/oRHsThgQdzk?feature=shared

 

댓글