remove a selection from a dropdown list box.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi I have a dropdown listbox that is populated by binding to a dataset.
Anyhow just wondering if anyone knows how to remove one of the items in the
dropdown list in code after the dataset has populated it?
thanks.
 
Hi Paul,

You can use the RemoveAt method for that. You need to locate the item and
get its index. Using the index value you can remove it.

Some code below. Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
DropDownList1.DataSource = CreateDataSource()
DropDownList1.DataTextField = "StringValue"
DropDownList1.DataValueField = "IntegerValue"
DropDownList1.DataBind()
End If
End Sub
Private Sub Button1_Click _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
DropDownList1.Items.RemoveAt _
(DropDownList1.Items.IndexOf _
(DropDownList1.Items.FindByText("Item 2")))
End Sub
Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 4
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource

<form id="Form1" method="post" runat="server">
<P>
<asp:DropDownList id="DropDownList1"
runat="server"></asp:DropDownList></P>
<P>
<asp:Button id="Button1" runat="server" Text="Remove
2"></asp:Button></P>
</form>
 
ok thanks this is what I was looking for.

Ken Cox said:
Hi Paul,

You can use the RemoveAt method for that. You need to locate the item and
get its index. Using the index value you can remove it.

Some code below. Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
DropDownList1.DataSource = CreateDataSource()
DropDownList1.DataTextField = "StringValue"
DropDownList1.DataValueField = "IntegerValue"
DropDownList1.DataBind()
End If
End Sub
Private Sub Button1_Click _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
DropDownList1.Items.RemoveAt _
(DropDownList1.Items.IndexOf _
(DropDownList1.Items.FindByText("Item 2")))
End Sub
Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 4
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource

<form id="Form1" method="post" runat="server">
<P>
<asp:DropDownList id="DropDownList1"
runat="server"></asp:DropDownList></P>
<P>
<asp:Button id="Button1" runat="server" Text="Remove
2"></asp:Button></P>
</form>
 
Hi Paul,

I forgot to mention that the code sample isn't wrapped in a try...catch. Not
sure what happens if the index item doesn't exist so you might want to watch
for that.

Ken

Paul said:
ok thanks this is what I was looking for.

Ken Cox said:
Hi Paul,

You can use the RemoveAt method for that. You need to locate the item and
get its index. Using the index value you can remove it.

Some code below. Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
DropDownList1.DataSource = CreateDataSource()
DropDownList1.DataTextField = "StringValue"
DropDownList1.DataValueField = "IntegerValue"
DropDownList1.DataBind()
End If
End Sub
Private Sub Button1_Click _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
DropDownList1.Items.RemoveAt _
(DropDownList1.Items.IndexOf _
(DropDownList1.Items.FindByText("Item 2")))
End Sub
Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 4
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource

<form id="Form1" method="post" runat="server">
<P>
<asp:DropDownList id="DropDownList1"
runat="server"></asp:DropDownList></P>
<P>
<asp:Button id="Button1" runat="server" Text="Remove
2"></asp:Button></P>
</form>


Paul said:
Hi I have a dropdown listbox that is populated by binding to a dataset.
Anyhow just wondering if anyone knows how to remove one of the items in
the
dropdown list in code after the dataset has populated it?
thanks.
 
sounds like a good idea, will put it in a try catch methods.

Ken Cox said:
Hi Paul,

I forgot to mention that the code sample isn't wrapped in a try...catch. Not
sure what happens if the index item doesn't exist so you might want to watch
for that.

Ken

Paul said:
ok thanks this is what I was looking for.

Ken Cox said:
Hi Paul,

You can use the RemoveAt method for that. You need to locate the item and
get its index. Using the index value you can remove it.

Some code below. Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
DropDownList1.DataSource = CreateDataSource()
DropDownList1.DataTextField = "StringValue"
DropDownList1.DataValueField = "IntegerValue"
DropDownList1.DataBind()
End If
End Sub
Private Sub Button1_Click _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
DropDownList1.Items.RemoveAt _
(DropDownList1.Items.IndexOf _
(DropDownList1.Items.FindByText("Item 2")))
End Sub
Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 4
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource

<form id="Form1" method="post" runat="server">
<P>
<asp:DropDownList id="DropDownList1"
runat="server"></asp:DropDownList></P>
<P>
<asp:Button id="Button1" runat="server" Text="Remove
2"></asp:Button></P>
</form>


Hi I have a dropdown listbox that is populated by binding to a dataset.
Anyhow just wondering if anyone knows how to remove one of the items in
the
dropdown list in code after the dataset has populated it?
thanks.
 
Ken, Paul-
I just wanted to say thanks.
Found the answer to my question here.
Using VB.NET (forms) I came up with this:

lstSelFnd.Items.RemoveAt _
(lstSelFnd.Items.IndexOf(lstSelFnd.SelectedItems(0)))

Thanks again.

Chris Hagemaier
Systems Analyst
 
yep the RemoveAt seems like a pretty useful function. Just wondering if you
might know how to use a hot key with a .net web application? This is where
for example the user selects a key on the keyboard and it has the same effect
as clicking on a button on the form. Thinking may have to use Java script.
 
Negative. But it's a future enhancement I'm interested in
adding to my apps. Seems like questions get lost in the
maze of a billion answers out there.

Chris Hagemaier
Systems Analyst
 
Back
Top