开发者

When filling a combobox with data from a datatable, is there a way to 'get to' the other 'members' of the datatable?

开发者 https://www.devze.com 2023-03-22 00:05 出处:网络
Okay, I don\'t think this question has been asked here - or at least the search didn\'t find it... I have a combobox I\'m filling like so:

Okay, I don't think this question has been asked here - or at least the search didn't find it...

I have a combobox I'm filling like so:

DataTable companies = master.master_getCompanies();  // returns a DataTable
cboCompany.DataSource = companies;
cboCompany.DisplayMember = "CompanyName";
cboCompany.ValueMember = "SQLID";

This works fine

Later, after the user selects an item from the drop down, is there a way to get other columns from the previous datatable?

I am hoping to get somthing like this:

string foo = ((some_cast)cboCompany.SelectedItem).members["column_bar"];

I really have no idea if anything like this is possible - it just 'feels' like I should be able to do it.

I know I can keep the datatable in memory, and use the SQLID (ValueMember) that the combobox is giving me to re-lookup the other column - and I guess that's my fall-back solution if no one else has a better idea by morning :开发者_如何学编程)

Thanks for the help guys (and gals!)


You can try this (I tried in VS2010):

var row = (DataRowView)cboCompany.SelectedItem;

var value = row["{column name}"];

HTH


internal class Company
{
    public string CompanyName { get; set; }
    public string SQLID { get; set; }
    public string column_bar { get; set; }
}

...

    List<Company> list = new List<Company>();
    list.Add(new Company { CompanyName = "aaaaaa", SQLID = "ssssss", column_bar = "ddddddd" });
    list.Add(new Company { CompanyName = "zzzzzzz", SQLID = "xxxxxx", column_bar = "ccccccccc" });
    cboCompany.DataSource = list;
    cboCompany.DataBind();

    // ...
    string foo = ((List<Company>)cboCompany.DataSource).ElementAt(cboCompany.SelectedIndex).column_bar;
0

精彩评论

暂无评论...
验证码 换一张
取 消