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?
>
>