-
Oct 28th, 2010, 07:36 AM
#1
Thread Starter
Lively Member
[RESOLVED] checkedlistbox to check the values as per database
i have the below code
For i = 0 To Me.checkedlistbox1.Items.Count - 1
If reader("productname") = checkedlistbox1.SelectedItem(i).ToString Then
checkedlistbox1.SetItemCheckState(i, chkstate)
End If
from the checkedbox list i want the items in the checkedlistbox1 to be checked depending on the matching text frm the database (reader("productname"))
but its not working correctly
-
Oct 29th, 2010, 01:43 AM
#2
Re: checkedlistbox to check the values as per database
vb.net Code:
Dim productName = CStr(reader("productname")) For i = 0 To Me.checkedListBox1.Items.Count - 1 Dim item = CStr(Me.checkedListBox1.Item) Me.checkedListBox1.SetItemChecked(i, item = productName) Next
I wasn't aware that the CheckedListBox was supported on the CF.
-
Oct 29th, 2010, 06:21 AM
#3
Re: checkedlistbox to check the values as per database
Originally Posted by jmcilhinney
I wasn't aware that the CheckedListBox was supported on the CF.
You are right, it isn't:
http://msdn.microsoft.com/en-us/libr...edlistbox.aspx
Gary
-
Oct 30th, 2010, 12:48 AM
#4
Thread Starter
Lively Member
Re: checkedlistbox to check the values as per database
For i = 0 To Me.checkedlistbox1.Items.Count - 1
If checkedlistbox1.Items(i).ToString() = reader("PRODUCTNAME").ToString Then
checkedlistbox1.SetItemCheckState(i, CheckState.Checked)
End If
Next
by using above i get the data in reader("PRODUCTNAME").ToString but
checkedlistbox1.Items(i).ToString() does not givethe list of the item
instead when debugged it shows value as "System.Data.DataRowView"
any by using the code u provided i get
An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
Additional information: Conversion from type 'DataRowView' to type 'String' is not valid.
Last edited by svibuk; Oct 30th, 2010 at 12:57 AM.
-
Oct 30th, 2010, 01:04 AM
#5
Re: checkedlistbox to check the values as per database
This is an example of why you need to provide a full and clear description upfront. From that error I can tell that you have bound a DataTable to your CheckedListBox, which is information that you should have provided in the first place. My code was actually wrong anyway, as it should have been like this for just plain Strings added to the list:
vb.net Code:
Dim productName = CStr(reader("productname")) For i = 0 To Me.checkedListBox1.Items.Count - 1 Dim item = CStr(Me.checkedListBox1.Items(i)) Me.checkedListBox1.SetItemChecked(i, item = productName) Next
Now that I know that it's actually not just Strings in the list but a bound DataTable, I can tell you that the code should actually be:
vb.net Code:
Dim productName = CStr(reader("productname")) For i = 0 To Me.checkedListBox1.Items.Count - 1 Dim item = Me.checkedListBox1.GetItemText(i) Me.checkedListBox1.SetItemChecked(i, item = productName) Next
By the way, are you sure that this is a mobile application?
-
Oct 30th, 2010, 01:38 AM
#6
Thread Starter
Lively Member
Re: checkedlistbox to check the values as per database
hi thanks
?Me.checkedlistbox.GetItemText(i)
"0"
i get the index value & from database i get the product name
i a sorry for not giving u the idea abt datatable
& its not a mobile application , after registarting by mistake i posted in teh forum page that was opened
-
Oct 30th, 2010, 03:03 AM
#7
Re: checkedlistbox to check the values as per database
If you post in the wrong forum then you should let the moderators know so that they can move the thread. You can do that using the Report Post icon on each post or by contacting a moderator directly. The home page shows who moderates each forum. I've notified them this time but please do so yourself in future.
My code was a little bit off again. That's what I get for not typing into an IDE first. It should have been:
vb.net Code:
Dim productName = CStr(reader("productname")) For i = 0 To Me.checkedListBox1.Items.Count - 1 Dim item = Me.checkedListBox1.Items(i) Dim itemText = Me.checkedListBox1.GetItemText(item) Me.checkedListBox1.SetItemChecked(i, itemText = productName) Next
-
Oct 30th, 2010, 04:22 AM
#8
Re: checkedlistbox to check the values as per database
-
Nov 2nd, 2010, 01:38 AM
#9
Thread Starter
Lively Member
Re: checkedlistbox to check the values as per database
thanks
thnaks for u r help
withslight modification i culd resolve teh checkedlistbox checked issue
Dim productName = CStr(reader("productname"))
For i = 0 To Me.checkedListBox1.Items.Count - 1
Dim item = Me.checkedListBox1.Items(i)
Dim itemText = Me.checkedListBox1.GetItemText(item)
if itemText = productName then
Me.checkedListBox1.SetItemChecked(i, CheckState.Checked)
end if
Next
-
Jun 5th, 2017, 03:47 AM
#10
Registered User
Re: checkedlistbox to check the values as per database
You can try using 2 alternatives also :
1. Using SelectedValue for SetItemChecked :
While DataReader.Read ()
checkedListBox1.SelectedValue = dataReader.Item("DATABASE_VALUE")
If checkedListBox1.SelectedIndex > -1 Then
checkedListBox1.SetItemChecked(checkedListBox1.SelectedIndex, True)
End If
End While
2. Retrieve the Checkbox Datasource and check with your live database values :
Dim dtTempNewDataSet as New DataSet = checkedListBox1.DataSource
While dataReader.Read()
For i = 0 To dtTempNewDataSet.Rows.Count - 1
If dtTempNewDataSet .Rows(i).Item("YOUR_VALUE_MEMBER") = dataReader.Item("DATABASE_VALUE") Then
checkedListBox1.SetItemChecked(i, True)
End If
Next
End While
-
Jun 5th, 2017, 09:54 AM
#11
Re: [RESOLVED] checkedlistbox to check the values as per database
@sachip, this thread is almost 7 years old.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|