How to populate listbox dynamically with SQL values

…衆ロ難τιáo~ 提交于 2019-12-05 15:17:40

Why not use the DataTable as DataSource:

public mainForm()
        {
            InitializeComponent();
            SqlConnection conn = new SqlConnection("Data Source=DBELL;Initial Catalog=part_table;Integrated Security=True");
            conn.Open();
            DataSet ds = new DataSet();
            SqlDataAdapter adapter = new SqlDataAdapter(
            "SELECT part_num from customParts", conn);
            adapter.Fill(ds);
            this.listParts.DataSource = ds.Tables[0]; 
            this.listParts.DisplayMember = "part_num"; 
        }

You should read up on DataSets or even better yet EntityFramework and data-binding.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!