c# Listbox help
-
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? -
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?A lot of this depends on how your data source is bound to the
ListBox
. When you update yourDataTable
like that, the changes may not be reflected in theListBox
. See theCurrencyManager
for more information about a way to refresh the bindings, like callingRefresh
on theCurrencyManager
you can get from theListBox.BindingContext
(inheritted from theControl
class).Microsoft MVP, Visual C# My Articles
-
A lot of this depends on how your data source is bound to the
ListBox
. When you update yourDataTable
like that, the changes may not be reflected in theListBox
. See theCurrencyManager
for more information about a way to refresh the bindings, like callingRefresh
on theCurrencyManager
you can get from theListBox.BindingContext
(inheritted from theControl
class).Microsoft MVP, Visual C# My Articles
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? -
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?Then create a
DataView
on the table and set that as theDataSource
.Microsoft MVP, Visual C# My Articles