Hello, I've a CheckListBox that contains item taken from a database table. For example: if I've 5 rows in my table, I've 5 CheckBox in my CheckListBox. this CheckListBox is relative to a Customer (for example). This customer could have 3 of these CheckBox checked.
When I load the dataset relative to the customer, I've a DataTable with the DataValueFields of the CheckBox. I'd like to bind these values to the checklistbox, so the correct CheckBox are checked. I could this also by hand, but I don't know how to find a control,
knowing his value. (FindControl requires ID, not value). Can you help me? Thank you.
With regard to binding the checkboxlist based on values in a database table check this out: Data Binding Multi-Record Web Server Controls http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskdatabindingmulti-recordcontrols.asp So
for example, assuming you are binding to DsCustomerOptions - this being the dataset, your code might look this:
To set the values based on customer selections check this out:
Setting the Selection in a List Web Server Control
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtsksettingselectioninlistbox.asp
You will have to get the values from the relevant customer table. Then set their selections like this:
CheckBoxList1.Items(0).Selected = True
So the first item in the checkboxlist items is selected. This corresponds to the first option from the dsCustomerOptions.
Also to find an item by value you can use this approach:
CheckListBox1.Items.FindByValue(Text)
Is this what you want to do??? Hope this helps Regards Basia
Yes I resolved in a similar, but less optimized, way: Public Shared Sub CheckCaratteristiche(ByVal clb As CheckBoxList, ByVal dtCar As DataTable) If Not dtCar Is Nothing Then Dim i As Integer Dim drCar As DataRow For Each drCar In dtCar.Rows If Not drCar("Cod_Caratteristica")
Is DBNull.Value Then For i = 0 To (clb.Items.Count - 1) If clb.Items(i).Value = drCar("Cod_Caratteristica") Then clb.Items(i).Selected = True End If Next End If Next End If End Sub Using Items.FindByValue is more efficient :) Thank you!
I tried that example but it dint work for me. Do I need to drop a checkolist control on the page at design time?? I want to avoid doing that. Heres my code anyways: Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim obj_clsDB As clsDB Dim DS As New DataSet() 'OBJECT OF CLASS THAT DOES SOME BASIC DATABASE FUNCTIONS. obj_clsDB = New clsDB() obj_clsDB.ConnString = "data source=xx;initial catalog=XX;password=XX;user id=XX;" 'FUNCTION TO RETURN A DATA-ADAPTER... THIS WORKS
PROPERLY obj_clsDB.GetDataAdapter("select * from CheckboxLoading").Fill(DS, "CheckboxLoading") 'FOR TESING TO SEE THE CONTENTS OF THE DATASET I USED THIS LABEL WITH THE 'ROWCOUNT... IT GAVE 6 RECORDS RETURNED Label1.Text = DS.Tables("CheckboxLoading").Rows.Count
ctrl_CBOX = New CheckBoxList() ctrl_CBOX.DataSource = DS ctrl_CBOX.DataMember = "CheckboxLoading" ctrl_CBOX.DataTextField = "CaptionStr" ctrl_CBOX.DataValueField = "selected" ctrl_CBOX.RepeatDirection = RepeatDirection.Vertical ctrl_CBOX.DataBind() End Sub
andy80
Member
415 Points
83 Posts
Is it possible to bind CheckListBox values?
Nov 03, 2003 07:52 AM|LINK
Basia1
Member
315 Points
63 Posts
Re: Is it possible to bind CheckListBox values?
Nov 03, 2003 10:59 AM|LINK
CheckListBox1.DataSource = DsCustomerOptions CheckListBox1.DataMember = "CustomerOptions" CheckListBox1.DataTextField = "Option" CheckListBox1.DataValueField = "option_id" CheckListBox1.DataBind()To set the values based on customer selections check this out: Setting the Selection in a List Web Server Control http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtsksettingselectioninlistbox.asp You will have to get the values from the relevant customer table. Then set their selections like this: So the first item in the checkboxlist items is selected. This corresponds to the first option from the dsCustomerOptions. Also to find an item by value you can use this approach: Is this what you want to do??? Hope this helps Regards Basiaandy80
Member
415 Points
83 Posts
Re: Is it possible to bind CheckListBox values?
Nov 03, 2003 12:23 PM|LINK
Log001
Member
10 Points
2 Posts
Re: Not Working
Nov 24, 2003 11:04 AM|LINK