Data bound list box displays empty item after adding new row to bound table

Data bound list box displays empty item after adding new row to bound table

Post by Torgeir St » Sun, 25 Jul 2004 08:28:12


I suppose there's no elegant solution to this (at least one that is
well-known), but for anyone else with a similar problem, this kludge
might work around it.

After adding the row, store the list box' display member string to a
variable. Then set the display member to null, and reset it to the
contents of the variable. This forces the list box to re-read the values
from the data source, including the proper display value of the newly
added row. If the list box reference is in a member variable, the code
now looks like this:

void AddItemTest(object sender, EventArgs e) {
_data.Tables["link"].Rows.Add(new object[] { 0, 0 });

// dirty hack to force update
string display_member = _item_list.DisplayMember;
_item_list.DisplayMember = null;
_item_list.DisplayMember = display_member;
}

Best regards,
Torgeir