如何在不使用适配器的情况下从组合框中的数据表添加数据 [英] how to add data from datatable in combobox without using adapter

查看:63
本文介绍了如何在不使用适配器的情况下从组合框中的数据表添加数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我必须从数据表column1和column2填充我的两个combobox1和combobox2.但我不想使用adapter.plz紧急帮助我.

hi all ,

i have to fill my two combobox1 and combobox2, from datatable column1 and column2. but i do not want to use adapter.plz help me out urgent.

thanks in advance!

推荐答案

DataTable dt = new DataTable();
            SqlDataReader rdr = null;
            try
            {
                SqlCommand cmd = new SqlCommand("select id, name from table", ClsMSetting.Con);
                 rdr = cmd.ExecuteReader();
                dt.TableName = "mydata";
                dt.Load(rdr);
                rdr.Close();
		//cb1 is a combo
		cb1.DisplayMember = "name";
            	cb1.ValueMember = "id";
                
            }
            catch (Exception Ex) { }


SqlCommand mycommand = new SqlCommand("select id, name from tablename", myCon);
                SqlDataReader dr = mycommand.ExecuteReader();

                while (dr.Read())
                {
                  id_cmb.Items.Add(dr.GetValue(0));
                  name_cmb.Items.Add(dr.GetValue(1));
}



但是它的nt正常工作.....组合框未填充唯一数据



But its nt working properly.....the combobox are not filling with unique data


SqlCommand mycommand = new SqlCommand("select id, name from tablename", myCon);
                SqlDataReader dr = mycommand.ExecuteReader();
 
                while (dr.Read())
                {
                  id_cmb.Items.Add(dr.GetValue(0));
                  name_cmb.Items.Add(dr.GetValue(1));
}



无论您想要什么,您的Above SQL查询都不满足您的条件...
如果要在组合框中显示不同的值,则将无法找到该记录的唯一ID(如果u具有相同的名称,但具有不同的ID).........因此,请使用唯一的Name更改数据库表,然后ID或不选择ID



Your Above SQL Query not satisfying your condition whatever you want......
If you want to display distinct value in combobox then you will not able to find unique ID for that records (if u have same name with different ID).........So either change your database table with unique Name and ID or don''t select ID

select distinct name from tablename


这篇关于如何在不使用适配器的情况下从组合框中的数据表添加数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆