remove a selection from a dropdown list box.

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.
 
K

Ken Cox [Microsoft MVP]

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

Guest

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

Ken Cox [Microsoft MVP]

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.
 
G

Guest

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.
 
C

Chris

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
 
G

Guest

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.
 
C

Chris

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top