Hi all.
I have to display a dialog adding a new DataRow into the DataTable
(say, dtMain).
One field (say, "Location") of dtMain is a reference to the "ID" field
of the other table (say, dtLocations) and I want this field to be
shown on the dialog form as a list of values of all "Name" fields of
dtLocations, not as a list of "ID" numbers.
I bind as follows:
// Making referenced data displayable by their names
dlgEditor.comboBoxLocation.DataSource = dtLocations;
dlgEditor.comboBoxLocation.DisplayMember = "Name";
dlgEditor.comboBoxLocation.ValueMember = "ID";
The other requirement is that initially I have to display the empty
Location field on the dialog form. dtLocations does not contain a row
with empty "Name" value.
Of course, I would be glad to do
dlgEditor.comboBoxLocation.SelectedValue = ""
or something like this but I get a natural exception.
This is what I do.
Before I show the adding dialog form, I add the temporary DataRow into
dtLocations with empty "Name" field:
DataRow rowWithEmpty = dtLocations.NewRow();
dtLocations.Rows.Add(rowWithEmpty);
dlgEditor.comboBoxLocation.SelectedValue = DBNull.Value; // or ""
Then I show the dialog, everything looks fine.
After the dialog isclosed, I delete this temporary row from the
dtLocations:
dtLocations.Rows.Remove(rowWithEmpty);
This works (the only thing I don't like is that this temporary empty
value is available among combo box values) but I am not sure I did
what I had to do in this situation (I am a newcommwr in .NET).
Is there any smarter decision?
Thanks,
Dmytro Pavlov.