473,386 Members | 1,721 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.

Force Listbox to Refresh

I have button that deletes items from a SQL datatable. The items are
displayed for the user in listbox, which is bound to the dataset. The
code works just fine insofar as it deletes the correct record from the
dataset and the datasource. What I need to make it do is to refresh
the listbox so that it reflects that the record has been deleted.
Right now, the listbox removes the first item from the list and leaves
the deleted item displayed. If I close the form and reopen it, all
appears as it should (e.g. the correct item is gone and the listbox
displays correctly). How can I force the listbox to refresh as if the
form were re-loading.?

Here is the code:

Dim strSQL As String
strSQL = "DELETE Category WHERE Category = @Cat"
Dim cmdDelete As New SqlCommand(strSQL, cn)
cmdDelete.Parameters.AddWithValue("@Cat",
lbCat.SelectedValue)

Try
cn.Open()
Dim intRecordsAffected As Integer
intRecordsAffected = cmdDelete.ExecuteNonQuery()
If intRecordsAffected = 1 Then
CategoryBindingSource.RemoveCurrent()
CategoryBindingSource.MoveFirst()

End If
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
cn.Close()
End Try

Thanks a lot,
Randy

May 17 '07 #1
5 20137
Randy mostly is this the most simple one

listbox.datasource = nothing
listbox.datasource = thedatasource

Cor

"Randy" <sp***********@gmail.comschreef in bericht
news:11**********************@y80g2000hsf.googlegr oups.com...
>I have button that deletes items from a SQL datatable. The items are
displayed for the user in listbox, which is bound to the dataset. The
code works just fine insofar as it deletes the correct record from the
dataset and the datasource. What I need to make it do is to refresh
the listbox so that it reflects that the record has been deleted.
Right now, the listbox removes the first item from the list and leaves
the deleted item displayed. If I close the form and reopen it, all
appears as it should (e.g. the correct item is gone and the listbox
displays correctly). How can I force the listbox to refresh as if the
form were re-loading.?

Here is the code:

Dim strSQL As String
strSQL = "DELETE Category WHERE Category = @Cat"
Dim cmdDelete As New SqlCommand(strSQL, cn)
cmdDelete.Parameters.AddWithValue("@Cat",
lbCat.SelectedValue)

Try
cn.Open()
Dim intRecordsAffected As Integer
intRecordsAffected = cmdDelete.ExecuteNonQuery()
If intRecordsAffected = 1 Then
CategoryBindingSource.RemoveCurrent()
CategoryBindingSource.MoveFirst()

End If
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
cn.Close()
End Try

Thanks a lot,
Randy

May 18 '07 #2
"Cor Ligthert [MVP]" <no************@planet.nlschrieb:
Randy mostly is this the most simple one

listbox.datasource = nothing
listbox.datasource = thedatasource
.... which can be written as 'ListBox1.DataSource = TheDataSource' too. It's
not necessary to set the property to 'Nothing' there.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

May 18 '07 #3
Herfried,

You are right, I thought that I had a bad expirience in that, but accoording
all my writing about setting to nothing it would be crazy.

Cor

"Herfried K. Wagner [MVP]" <hi***************@gmx.atschreef in bericht
news:uX**************@TK2MSFTNGP02.phx.gbl...
"Cor Ligthert [MVP]" <no************@planet.nlschrieb:
>Randy mostly is this the most simple one

listbox.datasource = nothing
listbox.datasource = thedatasource

... which can be written as 'ListBox1.DataSource = TheDataSource' too.
It's not necessary to set the property to 'Nothing' there.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

May 18 '07 #4
OK, as simple as this looks, it isn't working for me. Since my
original post, I have changed my databinding so that I am now binding
the listbox to a dataview (so that I can sort the data in the
listbox). Here is my current code, which is intended to incorporate
your suggestions:

Dim strSQL As String
strSQL = "DELETE Category WHERE Category = @Cat"
Dim cmdDelete As New SqlCommand(strSQL, cn)
cmdDelete.Parameters.AddWithValue("@Cat",
lbCat.SelectedValue)

Try
cn.Open()
Dim intRecordsAffected As Integer
intRecordsAffected = cmdDelete.ExecuteNonQuery()
If intRecordsAffected = 1 Then
Dim vueCat As New DataView
With vueCat
.Table = ds.Category 'ds is my dataset
.Sort = "Category"
.RowStateFilter = DataViewRowState.CurrentRows
End With

With lbCat
.DataSource = vueCat
.DisplayMember = "Category"
.ValueMember = "Category"
End With
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
cn.Close()
End Try

I think that my problem has something to do with the
DataRowViewState. I've played around with this setting and achieved
different results. However, none of the options works in the various
scenarios that I need to update the binding with (Insert, Delete,
Update).

Any advice is appreciated.
Randy

May 20 '07 #5
Just to close out this post, I resolved the issue by simply filling my
dataadapter. All the stuff inside the If-Then statement is gone,
replaced with da.fill.

Thanks for the help, Cor and Herfried.

May 21 '07 #6

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

Similar topics

1
by: Charles Soto | last post by:
I've got a main loop script that calls two other scripts that do no user interaction. All they do is send a couple of mysql update statements. Then they use header() to call the main loop again. ...
19
by: Jay | last post by:
I'm trying to refresh browser when some new information is available in DB. is there any way to force to refresh browser? Thanks. -Kev
1
by: Tariq | last post by:
Hi, I have a list box that is bound to an arraylist, but when the data changes the listbox does not refresh. ----------------------------------------------------------------------------- al =...
1
by: Marco Maroni | last post by:
How to force image refresh on client browser ? Is ti possible to force the refresh of the same image (tha was changed server-side) to the client, without user press Contrl+F5 in IE ? - Marco
6
by: Rémi Lavoie | last post by:
Hi, I'm adding items in a listbox automatically. When my listbox is full, the scrollbar appears but i can't set my listbox to see the new items. I would like that my scrollbar follow the new...
10
by: Fred Nelson | last post by:
Hi: I have a VB.NET web application and I need to find a way to cause a page refresh from within my application. Does anyone know how to force the browser to refresh the current page? ...
1
by: Randy | last post by:
I have button that deletes items from a SQL datatable. The items are displayed for the user in listbox, which is bound to the dataset. The code works just fine insofar as it deletes the correct...
1
by: ronpenrose | last post by:
I have a listbox bound to an access database. In my ASP form, I select a list item to delete from the database, click Delete button. The record is deleted from the database but I cannot get the...
3
by: COHENMARVIN | last post by:
I have a button in my asp.net page (actually its in a user control with a .ASCX suffix) called btnGetData. I'm trying to make it force a refresh of my page. So what I did was make it call...
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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.