Hướng dẫn load dữ liệu combobox từ csdl năm 2024

em load dữ liệu từ database vào combobox bằng cách này

cbo_maKhoa.ValueMember = "MaKhoa"; string sql = "select * from Khoa"; cbo_maKhoa.DataSource= ClKetNoics.getData[sql];

thì cái combobox của em khi load lên nó hiện luôn cái giá trị dòng thứ 1

em muốn khi load lên dòng 1 nó là rỗng không có giá trị thì phải làm sao

em mới tập C# nên còn rất yếu mong được mọi người giúp đỡ

tks mọi người trước

using SoTayDongVat.DTO;  
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
using System.Data;
namespace SoTayDongVat.DAO  
{  
    public class ComboxBoDAO  
    {  
        private static ComboxBoDAO instance;
        public static ComboxBoDAO Instance  
        {  
            get {if [instance== null] instance = new ComboxBoDAO[]; return ComboxBoDAO.instance; }  
            private set { ComboxBoDAO.instance = value; }  
        }  
        private ComboxBoDAO[] { }
        public List GetlistComboxBo[]  
        {  
            List list = new List[];
            string query = "Select * from BO";
            DataTable data = DataProvider.Instance.ExecuteQuery[query];
            foreach [DataRow item in data.Rows]  
            {  
                ComboxBo comboxbo = new ComboxBo[item];  
                list.Add[comboxbo];  
            }  
            return list;  
        }  
    }  
}  

  1. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; namespace SoTayDongVat.DTO {

    public class ComboxBo  
    {  
        public ComboxBo [string MaBo, string TenBo]  
        {  
            this.MaBo1 = MaBo;  
            this.TenBo1= TenBo;  
        }  
        public ComboxBo[DataRow row]  
        {  
            this.MaBo1 = row["MaBo"].ToString[];  
            this.TenBo1 = row["TenBo"].ToString[];  
        }  
        private string TenBo;  
        public string TenBo1  
        {  
            get { return TenBo; }  
            set { TenBo = value; }  
        }  
        private string MaBo;  
        public string MaBo1  
        {  
            get { return MaBo; }  
            set { MaBo = value; }  
        }  
    }  
    
    }

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.SqlClient; using System.Configuration; using SoTayDongVat.DTO; using SoTayDongVat.DAO; namespace SoTayDongVat {

    public partial class FrmMain : Form  
    {  
        public FrmMain[]  
        {  
            InitializeComponent[];  
        }  
        private void btnExit_Click[object sender, EventArgs e]  
        {  
           Application.Exit[];  
        }  
        private void FrmMain_Load[object sender, EventArgs e]  
        {  
            Hienthi[];  
            LoadcomboxBo[];  
            LoadcomboxNhom[];  
        }  
        public void LoadcomboxBo[]  
        {  
            List listComboxbo = ComboxBoDAO.Instance.GetlistComboxBo[];  
            cbbTimBo.DataSource = listComboxbo;  
            cbbTimBo.DisplayMember = "TenBo";  
        }  
        public void LoadcomboxNhom[]  
        {  
            List listComboxnhom = ComboxnhomDAO.Instance.GetlistComboxNhom[];  
            cbbTimNhom.DataSource = listComboxnhom;  
            cbbTimNhom.DisplayMember = "TenNhom";  
        }  
        public void Hienthi[]  
        {  
                string Query = ["Select TenLoai, TapTinh, NoiSong, KhuVuc, TuyetChung, Soluong from LOAI  "];  
                dataGridView1.DataSource = DataProvider.Instance.ExecuteQuery[Query];  
        }  
    }  
    
    }

    mình đổ dữ liệu như v ko biết đã đúng chưa mình làm theo cách trong bài lập trình phần mềm quản lý bán cafe nhưng lúc chạy vẫn ko hiên lên đc combobox mặc dù đã thêm DisplayMemBer

SqlDataAdapter adapter = new SqlDataAdapter[sql, demo3Layer.Properties.Settings.Default.apConnectionString];

adapter.FillSchema[result, SchemaType.Source];

adapter.Fill[result];

}

catch [Exception ex]

{

MessageBox.Show[Error.AnErrorHasBeenOccured + "\r\n" + ex.Message];

}

return result;

}

/*

* @todo read database table 's structure

* */

protected static DataTable readStructure[string tableName]

{

DataTable result = new DataTable[];

string sql = "SELECT * FROM " + tableName;

try

{

SqlDataAdapter adapter = new SqlDataAdapter[sql, demo3Layer.Properties.Settings.Default.apConnectionString];

adapter.FillSchema[result, SchemaType.Source];

}

catch [Exception ex]

{

MessageBox.Show[Error.AnErrorHasBeenOccured + "\r\n" + ex.Message];

}

return result;

}

/*

* @todo write to database

* */

protected static int write[DataRow dr]

{

int result = 0;

string sql = "SELECT * FROM " + dr.Table.TableName;

DataTable dt = dr.Table;

try

{

SqlDataAdapter adapter = new SqlDataAdapter[sql, demo3Layer.Properties.Settings.Default.apConnectionString];

SqlCommandBuilder cmdBuilder = new SqlCommandBuilder[adapter];

result = adapter.Update[dt];

}

catch [Exception ex]

{

MessageBox.Show[Error.AnErrorHasBeenOccured + "\r\n" + ex.Message];

}

return result;

}

/*

* @todo: excute a sql

* if insert, return indentity id

* */

protected static int executeNonQuery[string sql]

{

int count = 0;

using [SqlConnection conn = new SqlConnection[demo3Layer.Properties.Settings.Default.apConnectionString]]

Chủ Đề