By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,302 Members | 3,621 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,302 IT Pros & Developers. It's quick & easy.

Listbox, DataView, DataTable, and System.Data.DataRowView

P: n/a
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.

Aug 29 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
I changed the sort property from true (of which I set early on) to
false on the ListBox in VS2003 IDE and then things started working as
they should. It even got rid of the "Cannot modify the Items
collection when the DataSouce property is set" exception I was seeing
too.

Weird!!!

Aug 30 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.