Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. c# Listbox help

c# Listbox help

Scheduled Pinned Locked Moved C#
csharpdatabasehelpquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    John Baird
    wrote on last edited by
    #1

    I'm pulling out what little remaining hair I have again. I have a list box with 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 insert 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 the new row 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?

    H 1 Reply Last reply
    0
    • J John Baird

      I'm pulling out what little remaining hair I have again. I have a list box with 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 insert 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 the new row 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?

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      A lot of this depends on how your data source is bound to the ListBox. When you update your DataTable like that, the changes may not be reflected in the ListBox. See the CurrencyManager for more information about a way to refresh the bindings, like calling Refresh on the CurrencyManager you can get from the ListBox.BindingContext (inheritted from the Control class).

      Microsoft MVP, Visual C# My Articles

      J 1 Reply Last reply
      0
      • H Heath Stewart

        A lot of this depends on how your data source is bound to the ListBox. When you update your DataTable like that, the changes may not be reflected in the ListBox. See the CurrencyManager for more information about a way to refresh the bindings, like calling Refresh on the CurrencyManager you can get from the ListBox.BindingContext (inheritted from the Control class).

        Microsoft MVP, Visual C# My Articles

        J Offline
        J Offline
        John Baird
        wrote on last edited by
        #3

        Okay. I traced through the previously posted code and the data is actually being inserted into the table correctly and at the correct position. It is the DefaultView which is causing the list to display in the wrong order. I added the following to the method: CurrencyManager cmSelect = (CurrencyManager)this.lstSelected.BindingContext(dtSelect); cmSelect.Refresh(); It runs without error, but nothing changes. Can anyone suggest another approach?

        H 1 Reply Last reply
        0
        • J John Baird

          Okay. I traced through the previously posted code and the data is actually being inserted into the table correctly and at the correct position. It is the DefaultView which is causing the list to display in the wrong order. I added the following to the method: CurrencyManager cmSelect = (CurrencyManager)this.lstSelected.BindingContext(dtSelect); cmSelect.Refresh(); It runs without error, but nothing changes. Can anyone suggest another approach?

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          Then create a DataView on the table and set that as the DataSource.

          Microsoft MVP, Visual C# My Articles

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups