Oblivious Sage
Oblivious Sage

Reputation: 3405

How can I get a data source's value from a data binding?

I have a DevExpress LookupEdit (a dropdown, basically). Under certain circumstances the DataTable the LookupEdit is bound to can load a value from the database that does not appear in the list of values the LookupEdit can display. In this case the LookupEdit's EditValue property is set to DBNull when it reads from its data binding, but the value in the DataTable is preserved.

I would like a way to get the value from the DataTable using the control's DataBindings property, not by referencing it directly. That is, I would like to reference it via MyLookupEdit.DataBindings[0].SomethingSomething rather than via MyDataTable[MyLookupEditColumn]. That way I can get at it in an event handler (where I'm given the calling LookupEdit) and re-use that event handler for different LookupEdits (which bind to different columns in my DataTable).

I'd like to do something like this:

Binding bind = (sender as LookupEdit).DataBindings[0];
DataTable table = bind.DataSource as DataTable;
object data = table[binding.DataMemberInfo.BindingMember];

... but BindingMember is a string and I need a DataColumn.

Upvotes: 2

Views: 5940

Answers (1)

Oblivious Sage
Oblivious Sage

Reputation: 3405

I was on the right track, there were just several more steps. This is what I needed:

Binding bind = (sender as Control).DataBindings[0];
DataTable table = (bind.DataSource as DataSet).Tables[0];
string table_column_name = bind.BindingMemberInfo.BindingMember;
string column_name = table_column_name.Split(new char[] { '.' })[1];
DataColumn column = table.Columns[table.Columns.IndexOf(column_name)];
object data = table.Rows[0][column];

Upvotes: 1

Related Questions