게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
c# combobox에 입력된 data를 binding하고싶은데요...ㅜ
게시물ID : programmer_9411짧은주소 복사하기
작성자 : 건들면웁니다
추천 : 0
조회수 : 3532회
댓글수 : 4개
등록시간 : 2015/04/17 11:29:48
옵션
  • 본인삭제금지

안녕하세요
개인적으로 공부중인데 물어볼 데도 없고 막막해서 프게분들께나마 여쭤봅니다ㅠ_ㅠ
주변에 C#하는 친구들이 하나도 없네요....

현재 textbox 2개에 값을 하나씩 (name, city)를 입력받고, button을 눌렀을 때 저장되고 다른 값을 넣으면 계속 추가되는 구조로 짜고있습니다.
그리고 combobox에 name이 출력되게끔 만들고 싶은데요.
stackoverflow나 인터넷을 뒤졌을 땐 databinding이 가장 효과적일 것 같았는데 너무 어렵네요 ㅠ.ㅠ

현 상태는 name과 city는 계속 추가가 되지만, combobox의 name을 선택하면 값이 하나씩 밀립니다...
예를 들어 index가 0인 name의 값이 '강호동' 이고 index가 1인 name의 값이 '유재석' 이면, 

제가 유재석까지 입력한 뒤에 combobox를 열어 강호동을 클릭하면 name에 한해서 '유재석'이라는 중복값을 끌고옵니다ㅠㅠ
제대로 binding이 안된거같은데..또 웃긴건 다시 한 번 강호동을 클릭하면 또 강호동이 나와요 ;ㅅ; 끙.....

어제도 하루종일 끙끙 앓았는데 오늘도 못하면 너무 힘들거같아요 
제가 방향성을 아예 잘못잡고있는건가요?ㅠㅠ




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ServerPaser
{
    public partial class ComboBox : Form
    {
        string city;
        string name;
        Dictionary<string, comboBoxData> list = new Dictionary<string, comboBoxData>();
        
public ComboBox()
        {
            InitializeComponent();
        }

        public class comboBoxData
        {
            public string name { get; set; }
            public string city { get; set; }
        }

        private void button1_Click(object sender, EventArgs e) //add click
        {
            list.Add(textBox1.Text, new comboBoxData(){
                name = textBox1.Text,
                city = textBox2.Text
            });
            comboBox1.DataSource = new BindingSource(list, null);

            comboBox1.DisplayMember = "Key";
            comboBox1.ValueMember = "Value";            
        }

        
        private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
        {
            name = comboBox1.SelectedText.ToString();
            city = comboBox1.SelectedValue.ToString();

            textBox1.Text = name;
            textBox2.Text = city;
        }
    }
}


전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호