Hello World

C#(WINFORM, WINCE, WINDOWS MOBILE) 실전 활용 예제소스 및 프로그램 개발

[C#]DataGridViewComboBoxColumn 예제

댓글 0

C#

2021. 4. 19.

DB 조회 후 datagridview 데이터 바인딩, 콤보박스 컬럼 및 사용여부 값 설정 예제

        string strDBFile = Path.Combine(Application.StartupPath, "db.s3db");

        string stringConnInfo = String.Format("Data Source={0}", strDBFile);

        dataGridView1.Columns.Clear();

        SQLiteConnection conn = new SQLiteConnection(stringConnInfo);

        conn.Open();

        StringBuilder sb = new StringBuilder();

        sb.AppendLine("SELECT COM_REG_NO AS 사업자등록번호, COM_NAME AS 사업자명, USE_YN AS 사용여부 FROM TB_ENTERPRISE");

        SQLiteDataAdapter SDA = new SQLiteDataAdapter();

        SDA.SelectCommand = new SQLiteCommand(sb.ToString(), conn);

        DataTable dt = new DataTable();

        SDA.Fill(dt);

        dataGridView1.DataSource = dt;

        DataGridViewComboBoxColumn USE_YN = new DataGridViewComboBoxColumn();

        USE_YN.Items.AddRange(new object[] {
        "Y",
        "N"});

        USE_YN.HeaderText = "사용여부";

        USE_YN.FlatStyle = FlatStyle.Flat;

        dataGridView1.Columns.Insert(2, USE_YN);

        dt.Dispose();

        SDA.Dispose();

        conn.Close();

        conn.Dispose();

        for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {
            dataGridView1.Rows[i].Cells[2].Value = dataGridView1.Rows[i].Cells[3].Value;
        }