Results 1 to 11 of 11

Thread: [RESOLVED] checkedlistbox to check the values as per database

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    73

    Resolved [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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: checkedlistbox to check the values as per database

    vb.net Code:
    1. Dim productName = CStr(reader("productname"))
    2.  
    3. For i = 0 To Me.checkedListBox1.Items.Count - 1
    4.     Dim item = CStr(Me.checkedListBox1.Item)
    5.  
    6.     Me.checkedListBox1.SetItemChecked(i, item = productName)
    7. Next
    I wasn't aware that the CheckedListBox was supported on the CF.

  3. #3
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: checkedlistbox to check the values as per database

    Quote Originally Posted by jmcilhinney View Post
    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

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    73

    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.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    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:
    1. Dim productName = CStr(reader("productname"))
    2.  
    3. For i = 0 To Me.checkedListBox1.Items.Count - 1
    4.     Dim item = CStr(Me.checkedListBox1.Items(i))
    5.  
    6.     Me.checkedListBox1.SetItemChecked(i, item = productName)
    7. 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:
    1. Dim productName = CStr(reader("productname"))
    2.  
    3. For i = 0 To Me.checkedListBox1.Items.Count - 1
    4.     Dim item = Me.checkedListBox1.GetItemText(i)
    5.  
    6.     Me.checkedListBox1.SetItemChecked(i, item = productName)
    7. Next

    By the way, are you sure that this is a mobile application?

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    73

    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

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    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:
    1. Dim productName = CStr(reader("productname"))
    2.  
    3. For i = 0 To Me.checkedListBox1.Items.Count - 1
    4.     Dim item = Me.checkedListBox1.Items(i)
    5.     Dim itemText = Me.checkedListBox1.GetItemText(item)
    6.  
    7.     Me.checkedListBox1.SetItemChecked(i, itemText = productName)
    8. Next

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: checkedlistbox to check the values as per database

    Moved To VB.NET

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    73

    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

  10. #10
    Registered User
    Join Date
    Jun 2017
    Posts
    1

    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

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    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
  •  



Click Here to Expand Forum to Full Width