How to order a Listbox

Asked

Viewed 573 times

0

I have a listbox and two buttons on the side, one to move the selected item up and the other down. However, the list is loaded by DataSource from a bank table, I wanted to sort the position of the list items, because after ordering I will save these items in another table.

I saw you have the moveNext, but it does not change the position, only changes the selection.

 private void btnSobe_Click(object sender, EventArgs e)
 {
     bdsCliente.MoveFirst();
 }

 private void btnDesce_Click(object sender, EventArgs e)
 {
     bdsCliente.MoveLast();
 }
  • http://stackoverflow.com/questions/4796109/how-to-move-item-in-listbox-up-and-down

  • http://stackoverflow.com/questions/15748949/move-up-move-down-buttons-for-listboxes-in-visual-studio

  • Is this in Webforms or Desktop? The . NET Webforms, is that it stimulates the use of POST for any user action - even if it involves interface.

1 answer

1

You can sort the list with Linq and then assign the sorted list as Listview source data again.

It would be something like this:

private void btnOrdenaListView_Click(object sender, EventArgs e)
{
     // Ordena a lista por um campo aleatório.
     var registrosOrdenados = listView.DataSource.OrderBy(x => x.Id);

     listView.DataSource = registrosOrdenados;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.