从特定表中的所需列填充列表框 [英] Populating a Listbox from the desired column in a particular table

查看:66
本文介绍了从特定表中的所需列填充列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void AutoradioButton1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("Server=CHRIS-PC\\SQLEXPRESS;Database=jasmineSql.sdf;" +
                    "Integrated Security=True");
            conn.Open();
            DataSet sds = new DataSet();
            SqlCommand scmd = new SqlCommand("SELECT Description FROM TblCategories", conn);
                        SqlDataAdapter sadptr = new SqlDataAdapter(scmd);
                        sadptr.Fill(sds, "TblCategories");   
listBox1.DataSource = sds;
      listBox1.DataBind();  
            }


从特定表的所需列填充列表框(我的数据库中有3个表)

请你能告诉我怎么回事.单击单选按钮时,我无法在listBox1的"TblCategories"列中看到输入的值.


注意:我正在获取ListBox1.Databindings();.不是listBox1.DataBind();
这是造成人口不足的原因吗?请帮忙.

谁能为我提供解决方案?
谢谢.


Populating a Listbox from the desired column in a Particular table (I have 3 table in a my database)

Please can you tell me what is going worng. I coudn''t see the Values entered in the column "TblCategories" in my listBox1 when I clicked the radio button.


Note: I am getting ListBox1.Databindings(); Not listBox1.DataBind();
Is this causing to not populate. Please help.

Can anyone provide me with a solution?
Thanks.

推荐答案



假设您要使用WinForms解决方案,并且"listbox1"是标准的ListBox控件,那么您的问题是-除了最后一行之外,这阻止了代码的编译-错误的DataSource.

数据集是一组表,行,关系...,因此它没有用做数据源.如果要显示数据集中特定表中的特定列,则必须指定它.所以用
Hi,

assuming you''re asking for a WinForms solution and your "listbox1" is a standard ListBox control, your problem is - apart from the last line, which prevents your code from getting compiled - the wrong DataSource.

A DataSet is a set of Tables, Rows, Relations ... so it is of no use as a DataSource. If you want to display a specific column from a specific table from your DataSet, you must specify this. So use
this.listBox1.DataSource = sds.Tables["TblCategories"];
this.listBox1.DisplayMember = "Description";


代替


instead of

listBox1.DataSource = sds;


也许您应该考虑使用使用"块来保护SqlConnection,SqlCommand和SqlAdapter的处置.

干杯
于尔根(Jürgen)


And maybe you should consider using "using"-blocks to secure the disposal of the SqlConnection, the SqlCommand and the SqlAdapter.

Cheers
Jürgen


这篇关于从特定表中的所需列填充列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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