2013-07-30 75 views
0

我有一个列表框,它是数据绑定到accdb文件并显示一列的内容,它链接到的dataBindingSource也被过滤 - 这可以正常工作(但可能会影响什么我即将问)。例如,我想知道如何从所选项目的全部记录中提取一个值。列表框当前显示的是姓氏 - 这就是你能看到的所有信息,我怎样才能提取未显示但存在于数据绑定源中的客户姓名?从列表框中获取数据源

这是代码中使用的填充列表框:

public frmCustomer(string Input) 
    { 
     InitializeComponent(); 
     this.customersTableAdapter.Fill(this.dSSystem.Customers); 
     this.catsTableAdapter.Fill(this.dSSystem.Cats); 

     // Display Customer Record 
     int lvRecIdx = customersBindingSource.Find("AccRef", Input); 
     customersBindingSource.Position = lvRecIdx; 

     // Fetch Cats Owned 
     catsBindingSource.Filter = ("CustRef = '" + Input + "'"); 
    } 

谢谢

+0

请同时给我们展示一些代码并确保为WPF/Asp.NET/WinForms附加标签,以便您得到正确答案。 –

+0

在这种情况下,您应该从数据源中选择2列访问数据库。你也应该选择表的“主键”。 –

+0

@FabianBigler我目前没有任何代码显示,因为我不确定如何引用该记录中的另一个字段,如果有帮助,我添加了用于填充列表框的代码。 – MrDKOz

回答

1

一个ListBox包含两个成员组成:ValueMemberDisplayMember

你可以定义哪些你是从你的数据库查询填充一个简单的对象:

public class SomeItem 
{ 
     public int Key { get; set; } 
     public string DisplayText { get; set; } 
     public string Column1 { get; set; } 
     public string Column2 { get; set; } 
     ...etc... 
} 

你的实现可能看起来像这样(一些模型数据):

var items = new List<SomeItem>(); 
    var item = new SomeItem(); 
    item.Key ="key1"; 
    item.DisplayText = "value1"; 
    item.Column1 = "col1"; 
    item.Column2 = "col2"; 
    items.Add(item); 
    listBox1.DataSource = items; 
    listBox1.DisplayMember = "DisplayText"; //User will see your DisplayText 
    listBox1.ValueMember = "Key"; //The key which is unique and your Primary Key on your database 

在此基础上您选择的值,您可以查询您的物品并获得物品:

var key = (int)listBox1.SelectedValue; 
    foreach (var existingItem in items) 
    { 
      if (existingItem.Key == key) 
      { 
       //woohoo got it! 
       Debug.Print(existingItem.Column1) 
       Debug.Print(existingItem.Column2) 
      } 
    } 
+0

已排序。非常感谢你! – MrDKOz