473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

remove a selection from a dropdown list box.

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.
--
Paul G
Software engineer.
Nov 19 '05 #1
7 1426
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" <Pa**@discussions.microsoft.com> wrote in message
news:DB**********************************@microsof t.com...
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.
--
Paul G
Software engineer.

Nov 19 '05 #2
ok thanks this is what I was looking for.

"Ken Cox [Microsoft MVP]" wrote:
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" <Pa**@discussions.microsoft.com> wrote in message
news:DB**********************************@microsof t.com...
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.
--
Paul G
Software engineer.


Nov 19 '05 #3
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" <Pa**@discussions.microsoft.com> wrote in message
news:CF**********************************@microsof t.com...
ok thanks this is what I was looking for.

"Ken Cox [Microsoft MVP]" wrote:
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" <Pa**@discussions.microsoft.com> wrote in message
news:DB**********************************@microsof t.com...
> 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.
> --
> Paul G
> Software engineer.


Nov 19 '05 #4
sounds like a good idea, will put it in a try catch methods.

"Ken Cox [Microsoft MVP]" wrote:
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" <Pa**@discussions.microsoft.com> wrote in message
news:CF**********************************@microsof t.com...
ok thanks this is what I was looking for.

"Ken Cox [Microsoft MVP]" wrote:
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" <Pa**@discussions.microsoft.com> wrote in message
news:DB**********************************@microsof t.com...
> 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.
> --
> Paul G
> Software engineer.


Nov 19 '05 #5
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
*** Sent via Developersdex http://www.developersdex.com ***
Nov 19 '05 #6
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.

"Chris" wrote:
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
*** Sent via Developersdex http://www.developersdex.com ***

Nov 19 '05 #7
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
*** Sent via Developersdex http://www.developersdex.com ***
Nov 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: JL | last post by:
Does anyone have any ideas on this please? I don't know how to evaluate the no-blank selections against each other from a form as follows: . I have a form with 6 dropdown fields, each...
6
by: Mark | last post by:
I have two dropdown lists. Both have autopostback set to true. In both dropdowns, when you select an item from the list, it redirects to the Value property of the dropdown. Nothing fancy. ...
4
by: Paul | last post by:
I have a dropdown list box and a button on a web form, the autopost back is false for the dropdown list box and button. When the button is pushed the selection in the dropdownlist box is lost,...
5
by: plumba | last post by:
Hi all. I have two drop down menus, the first a list of Departments, the second a list of Sections. Each Department has a set of Setions, so the Sections dropdown contains complete list of all...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.