Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Listbox help
Message
From
05/05/2004 11:09:02
 
 
To
05/05/2004 08:47:41
John Baird
Coatesville, Pennsylvania, United States
General information
Forum:
ASP.NET
Category:
Other
Title:
Miscellaneous
Thread ID:
00901143
Message ID:
00901210
Views:
17
Wow, John ... that was a tough one, took me awhile to figure it out. But, here's what's going on. If you look at the table in the debugger, it appears correct. Your row is getting added at the correct spot in the table. However, the listbox is actually based on the DefaultView of the table. And if you look at the DefaultView, you'll see that the row is actually last instead of inserted where you put it in the table.

The only solution I can think of is to set a View as your DataSource instead of the Table. And have a sort column in this View to keep them in the order that you're moving them around in. Hmmm ... I didn't say that too well, but I hope you know what I mean.

~~Bonnie


>Hello:
>
>I'm pulling out my little remaining hair again. I have a list box with about 8 items, the selected item is highlighted at the top of the list. I press a button (cmdMoveDown) and wish to move the selected item 1 item down the list.
>
>I tried this:
>
>
>public void MoveDown()
>{
>     int index = this.lstSelected.SelectedIndex;
>     int newindex = index + 1;
>
>     DataTable dtSelect = (DataTable)this.lstSelected.DataSource;
>     DataRow dr = dtSelect.Rows[index];
>
>     dtSelect.Rows.Remove(dr);
>     dtSelect.Rows.InsertAt(dr, newindex);
>}
>
>
>this removes the item at index 0 just fine. But the select doesn't happen and there is a blank line at the bottom of the list.
>
>
>then i tried this variation:
>
>
>public void MoveDown()
>{
>     int index = this.lstSelected.SelectedIndex;
>     int newindex = index + 1;
>
>     DataTable dtSelect = (DataTable)this.lstSelected.DataSource;
>     DataRow dr = dtSelect.Rows[index];
>     DataRow drNew = dtSelect.NewRow();
>
>     foreach(DataColumn dc in dtSelect.Columns)
>        drNew[dc.ColumnName] = dr[dc.ColumnName];
>
>     dtSelect.Rows.Remove(dr);
>     dtSelect.Rows.InsertAt(drNew, newindex);
>}
>
>this variation removed the selected item and added it at the end of the list rather than at new index.
>
>I tried various permutations of this code and could get nothing to work. Any suggestions?
>
>
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Reply
Map
View

Click here to load this message in the networking platform