[已解决]具有C#中数据源的列表框 [英] [Solved] Listbox with Datasource in C#

查看:73
本文介绍了[已解决]具有C#中数据源的列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,它将使用查询描述将数据源对象绑定到列表框:

我的项目中有DataSource3:
这是下面的结构
数据表1
列1
但仍未填充列表框.
我试过了:
看到"ctr"的值是187
我接受了查询并遇到了Access查询向导.它返回187条记录.

我将Access 2007用作后端,将VS2008/C#用作前端.

I have the below code which will bind the data source object to list box using query describe :

I have DataSource3 in my project :
which is have below stucture
Datatable1
Column1
But still not populated the list box .
I tried this :
Saw the value of "ctr" which was 187
I took the query and ran into Access query wizard . it return 187 records.

I am using access 2007 as a backend and VS2008 / C# as a frontend.

bool bt = ControlRules.ClsConnect.Connect();
            String StrQuery;
            StrQuery = string.Empty;
            StrQuery = String.Concat(StrQuery, "SELECT Members.Name AS Column1 FROM Members ORDER BY Members.Name;");
            StringBuilder result = new StringBuilder();
            result.AppendFormat(StrQuery);
            StrQuery = string.Empty;
            OleDbDataAdapter adp = new OleDbDataAdapter(result.ToString(), ControlRules.ClsConnect.Conn);
            DataSet3 ds = new DataSet3();
            int ctr = adp.Fill(ds);
            
            //OleDbDataAdapter adp = new OleDbDataAdapter();
            //adp.Fill(ds, "DataTable1");
            //listBox1.DataSource = ds;
            //listBox1.DisplayMember = "DataTable1.Column1";
            //listBox1.ValueMember = "Column1";

            // Populate the list
            listBox1.DataSource = ds;
            // Define the field to be displayed
            listBox1.DisplayMember = "Column1";



请帮帮我.



Please help me out.

推荐答案

为什么要将StrQuery转换为result(StringBuilder)?

除非用于尚未发布的代码中,否则将完全浪费时间.

不知道DataSet3是什么,几乎不可能提出任何有用的建议.
Why are you converting StrQuery to result (StringBuilder)?

Unless it is used in code that you have not posted, it is a complete waste of time.

Without knowing what DataSet3 is, it is nearly impossible to make any useful suggestions.


就像亨利说的是什么DataSet3?它是从DataSet派生的类型还是只是一个错字.

也可以尝试:listbox1.DataSource = ds.Tables[0];
代替listbox1.DataSource = ds;.

还有注释的代码:
Just like Henry said what is DataSet3? Is it a type derived from DataSet or just a typo.

Also try: listbox1.DataSource = ds.Tables[0];
istead of listbox1.DataSource = ds;.

Also the commented code:
//OleDbDataAdapter adp = new OleDbDataAdapter();       
//adp.Fill(ds, "DataTable1");  
//listBox1.DataSource = ds;   
//listBox1.DisplayMember=  "DataTable1.Column1";            
//listBox1.ValueMember = "Column1";


是无用的,因为您已经在查询中仅选择了Column1


is kind of useless since you already select just Column1 in the query


如果您确定数据集不为空,请尝试使用此代码绑定列表框

If you are sure that the dataset is not empty,try this code to bind your listbox

public static void SetLookupBinding( ListControl toBind,
  object dataSource, string displayMember,
  string valueMember )
{
  toBind.DisplayMember = displayMember;
  toBind.ValueMember = valueMember;

  // Only after the following line will the listbox
  // receive events due to binding.
  toBind.DataSource = dataSource;
}


这篇关于[已解决]具有C#中数据源的列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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