Currency Manager returns last ComboBox item row

T

Tom

I've made something like that:

private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs
e)

{

CurrencyManager cm = (CurrencyManager)
this.BindingContext[((ComboBox)sender).DataSource];

DataRow dr = ((DataRowView) cm.Current).Row;

MessageBox.Show( dr[0].ToString());

}



comboBox1 is binded like this:



DataTable dt = new DataTable("table");

DataColumn ix = new DataColumn("ix",System.Type.GetType("System.String"));

DataColumn iy = new DataColumn("iy",System.Type.GetType("System.String"));

DataRow dr ;

dt.Columns.Add(ix);

dt.Columns.Add(iy);


dr = dt.NewRow();

dr[0] = "asterix";

dt.Rows.Add(dr);

dr = dt.NewRow();

dr[0] = "abladabla";

dt.Rows.Add(dr);

comboBox1.DataSource = dt;

comboBox1.DisplayMember = "ix";



The result is that if I choose abladabla from combobox, the sterix value is
displayed in a messagebox -> The last selected row is returned by the code
in SelectedIndexChanged. Don't know how to make this work "normal". Simply:
I choose asterix -> it displays asterix, I choose abladabla or whatsoever,
it displays it.
 
D

Dave Sexton

Hi Tom,

No need for a CurrencyManager. If you only want the selected text, then use
((ComboBox) sender).SelectedText, otherwise you can obtain the selected row
like this:

ComboBox comboBox1 = (ComboBox) sender;

DataRowView selectedRowView = (DataRowView) comboBox1.SelectedItem;

MessageBox.Show(selectedRowView[0]);

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top