I am experiencing some weird behaviors with the listbox and dataview.
Here is a boiled down code snippet of what I am trying to do.
<snippet>
string MENUID = "MenuId"
, ITEM = "Item"
, MENU_ENTRY_ID = "MenuEntryId";
private void populateMenu()
{
DataView dv = _ds.Tables["Menu"].DefaultView;
// have tried new DataView(_ds.Tables["Menu"]) too
dv.RowFilter = string.Format("{0} = '{1}'", MENUID, menuId);
// lbMenu.ValueMember = MENU_ENTRY_ID; // topic 1 for discussion
lbMenu.DataSource = dv;
lbMenu.DisplayMember = ITEM;
}
private void addNewEntry(int newValue)
{
// ... code checking to see if value already exists
int newMenuEntryId = GetController.AddNewRow();
// yes we are working with the same data source.
foreach (DataRow dr in dt.Rows)
{
if(Convert.ToInt32(dr[MENU_ENTRY_ID]) == newMenuEntryId)
{
dr[ITEM] = newValue;
// ... more related items
}
}
// populateMenu(); topic 2
}
</snippet>
Upon the form load event everything displays properly. I add a new
value that is to go into my list box for displaying to the end user. I
launch a new form and capture that new data point. An event is thrown
and I add the new item to the DataTable, as in the code snippet. The
interesting behavior begins.
Topic 1 discussion:
This discussion is for adding the new DataRow to the DataTable. If I
have this code uncommented and let the form move forward, my list box
shows the Value Members instead of the Display Members. If I leave the
code commented I see in the list box "System.Data.DataRowView" for my
results. (I am not able to have this piece of code after setting the
DataSouce because I receive an exception of "Cannot modify the Items
collection when the DataSouce property is set.") Regardless of the
commenting out, the ListBox shows that a new row in the underlying
source has been added.
Topic 2 discussion:
Why must I have to recreate the DataView and all the other trapings to
get the data to display properly? I should only have to set it once
and let the items do all the various updating.
Has anyone else seen this behavior? If so, where you able to overcome
the behavior without having to reset ListBox with a new DataView? I
have not started my work with deleting yet. Are there the same issues
there too?
Thank you in advance.