Hỏi đáp

Chia sẻ kiến thức, cùng nhau phát triển

C# đổ dữ liệu vào combobox

20:43 30-04-2017 14.767 lượt xem 6 bình luận 15:51 01-05-2017
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<ComboxBo> GetlistComboxBo()
        {
            List<ComboxBo> list = new List<ComboxBo>();

            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<ComboxBo> listComboxbo = ComboxBoDAO.Instance.GetlistComboxBo();
                cbbTimBo.DataSource = listComboxbo;
                cbbTimBo.DisplayMember = "TenBo";
            }
            public void LoadcomboxNhom()
            {
                List<Comboxnhom> 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 

     

Bình luận

Để bình luận, bạn cần đăng nhập bằng tài khoản Howkteam.

Đăng nhập
tranthiyen đã bình luận 23:44 10-10-2023

bạn ơi bạn fix được lỗi này chưa b

Darky đã bình luận 11:02 01-05-2017
         private void frm_Load(object sender, EventArgs e)
        {
            // Kết nối với Database thành công trước nhé
            tblNhom = xl.GetTbl("Nhom", "TenNhom");
            cmbNhom.DataSource = tblNhom;
            cmbNhom.DisplayMember = "TenNhom";
            cmbNhom.ValueMember = "MaLop"; 
        }
        
        // Lấy dữ liệu theo bảng và sắp xếp chúng theo 1 Field nào đó
        public DataTable GetTbl(string TblName, string FieldName)
        {
            string ChuoiSQL = "select * from " + TblName;
            if (FieldName != "") ChuoiSQL += " order by " + FieldName;
            DataTable tbl = new DataTable();
            OleDbDataAdapter da = new OleDbDataAdapter(ChuoiSQL, cn);
            da.Fill(tbl);
            return tbl;
        }

Bị comment tới 2 lần sr nhé

Darky đã bình luận 11:02 01-05-2017
         private void frm_Load(object sender, EventArgs e)
        {
            // Kết nối với Database thành công trước nhé
            tblNhom = xl.GetTbl("Nhom", "TenNhom");
            cmbNhom.DataSource = tblNhom;
            cmbNhom.DisplayMember = "TenNhom";
            cmbNhom.ValueMember = "MaLop"; 
        }
        
        // Lấy dữ liệu theo bảng và sắp xếp chúng theo 1 Field nào đó
        public DataTable GetTbl(string TblName, string FieldName)
        {
            string ChuoiSQL = "select * from " + TblName;
            if (FieldName != "") ChuoiSQL += " order by " + FieldName;
            DataTable tbl = new DataTable();
            OleDbDataAdapter da = new OleDbDataAdapter(ChuoiSQL, cn);
            da.Fill(tbl);
            return tbl;
        }

Mình hay làm theo cách này để lấy dữ liệu vào ComboBox(cmb) . Thường thì mình phân 3 lớp (Provider,Database,Controls) ,ở trên mình gôm lại 1 cái cho dễ nhìn . Bạn xem có giúp được gì không nhé :)

K9 SuperAdmin, KquizAdmin, KquizAuthor đã bình luận 03:55 01-05-2017

Bạn debug thì thấy có dữ liệu vào itemsource k bạn

Câu hỏi mới nhất