 |
|
 |
Note that even the DataSource, DisplayMember and ValueMember properties don't show up in VS's intellisense, they are there. ...... ...... anyDataSet globalDataSet = new anyDataSet(); CheckedListBox myChkListBox = new CheckedListBox(); .....
private void fillCheckedListBox() { anyDataSetTableAdapters.myTableTableAdapter ta = new anyDataSet TableAdapters.myTableTableAdapter(); ta.Fill(globalDataSet.myTable); myChkListBox .DataSource = globalDataSet.myTable; myChkListBox .DisplayMember = "myDisplayMember"; myChkListBox .ValueMember = "myValuMember"; myChkListBox .BindingContext = new BindingContext(); //This is the important part }
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Alejandro,
This solution is much simpler, but I'd like to know somethings from you:
1. What's the role of BindingContext in this code? 2. If you set the DataSource after the Display and Value members you also get an inconsistency in the displayed text? I tried with CheckedListBox and the custom one presented in this article, and both have this bug. Using ListBox, though, everything works fine. I tried to find out what's the difference between the implementation of ListBox and CheckedListBox but I counldn't get it. I believe there is some kind of event being raised after the regular code flow.
Both questions are important because I wouldn't like to teach my team how to make a "special bind" for one specific control, so, I'd rather create my own full functional control.
Thank you in advance.
Filipe
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
In databound mode this listbox appears to reset its Value property to 0 (zero) so all checked items appear unchecked if you call Dataset.AcceptChanges or Datatable.AcceptChanges in either the form's Load event or the form's Shown event.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi I have CheckListBox In ASP 2005 By VB How I Can Retrieve Selected Data From DataBase In Data Base I Have Three table 1- School -School_Code -School_Desc
2-Order -Order_Code -Order_Desc
3-OrdAndScho -Order_Code -School_Desc
One School Have One Or More Order
I Can insert the Value To The DataBase, But I Can’t Retrieve Please Help Me as soon As Possible
Note, I’m using SqlServer Data Base Thanks
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
 |
CheckedListBox does have Datasource, DisplayMember properties.
It just doesn't show up in Intellisense.
I have used below code and it is working.
FieldValuesListBox.DataSource = ds.Tables(0) FieldValuesListBox.DisplayMember = "Department"
VB Prog
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
If you just need the binding related properties, simply cast your CheckedListBox into ListBox. Since CheckedListBox inherits from ListBox, it will work. After the cast set the binding related properties and that's it. You can do all of this in the form load handler. Of course your solution is more readable (which is great), but it requires sub-classing for a very simple problem. I wonder why Microsoft have hidden these properties...
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
This is not a complete working example, but you can get the idea.
public partial class Form1 : Form { /// /// Just something to put in the list. /// It can be anything you like. /// class Fruit { protected string _name;
public Fruit(string name) { _name = name; }
public string Name { get { return _name; } } }
/// /// The list of objects to be used as a data source. /// We use a 'BindingList' so 'item changed' events are fired /// when we update the data source. This will cause the /// GUI to refresh automatically when we change the data source. /// This is classic MVC in action. /// private BindingList _fruitsDs = new BindingList();
public Form1() { InitializeComponent();
// Add some items to the data source. _fruitsDs.Add(new Fruit("apple")); _fruitsDs.Add(new Fruit("grapes")); _fruitsDs.Add(new Fruit("pear")); // Binding 'trick'. ListBox fruitListAsListBox = (ListBox)_fruitList; fruitListAsListBox.DataSource = _fruitsDs; fruitListAsListBox.DisplayMember = "Name"; }
private void button1_Click(object sender, EventArgs e) { // Change the list after we set up the binding. // If the data source was a simple 'List', this wouldn't // have caused the GUI to refresh. _fruitsDs.Add(new Fruit("banana")); } }
Shay.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Should be of course:
private BindingList<Fruit> _fruitsDs = new BindingList<Fruit>();
Haven't notice the HTML editor removed my <>...
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi, I like your solution to make the checkedlistbox bindable. But if I understand correctly, your example is expecting a bitwise coded relation between the two tables. This is very nasty for a simple relational database application. Do you have a tip or solution for this? Or did miscrosoft already solve this for us?
thankx, edgar
epuy@xs4all.nl
|
| Sign In·View Thread·PermaLink | 1.50/5 (2 votes) |
|
|
|
 |
|
|
 |
|
 |
I was really pleased when I found this article. I was trying to create a similar control, and had followed quite a similar approach but missed a couple of things.
I still seem to be having a problem though - Something really weird is happening. As soon as I have tabbed into or clicked on the control, I cannot seem to get out of it again.
It seems to still be working, but everything else, including the form it is on is locked up.
Has anyone else seen this (and hopefully resolved it)?
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
 |
I just can get the text in the selected items. But i need to get the VALUE of the checked item that set int the databinding. May you help me?
|
| Sign In·View Thread·PermaLink | 1.13/5 (7 votes) |
|
|
|
 |
|
 |
Hi,
I didn't test it or anything, so i may be totally wrong. But i have this idea that passing an array or a list of object as the single bound property (Value) would do the job in a more transparent way? The elements of the array would then be the selected items (or their ValueMember properties).
Also, i think that implementing the Value property of the control as IEnumerable or IList would allow the bound object to implement its binding property in any specialized way. So you should not be forced to use something generic as object[] or List<object>.
But again, i haven't tested my idea. And it's a nice article anyway.
Grtz, Phil
Philippe Dykmans Software developpement University of Antwerp
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
 |
Hello Eraghi,
As i was trying my idea in a separate project, turned out that there are a few problems i didn't immediately think of As i said, i didn't test it before. I'm gonna do a bit more thinking and trying on this. Will be back soon!
Regards, Philippe
Philippe Dykmans Software developpement University of Antwerp
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
 |
First of thanks for the great article - it really helped me to have a control I needed.
I've converted the code to VB (I used 2005 Express, but the code will probably work in previous versions, but I haven't tested that), and added a few properties.
I noticed you left out ValueMember, so I added that in as otherwise the SelectedValue property doesn't work.
I also added SelectedValues and CheckedValues to return arrays of the Selected/Checked values as determined by the ValueMember.
Imports System.ComponentModel Imports System.Drawing.Design
Public Class ExCheckedListBox Inherits CheckedListBox
Private _value As Integer
Public Property Value() As Integer Get Try Dim poweredNumber As Integer = 1 For i As Integer = 0 To Me.Items.Count - 1 If Me.GetItemChecked(i) Then Me._value = Me._value Or poweredNumber ElseIf Me._value And poweredNumber <> 0 Then Me._value -= poweredNumber End If poweredNumber *= 2 Next Catch ex1 As ArgumentException Throw ex1 Catch ex As Exception Throw ex End Try Return Me._value End Get Set(ByVal value As Integer) Me._value = value Try Dim poweredNumber As Integer = 1 For i As Integer = 0 To Me.Items.Count - 1 If Me._value And poweredNumber <> 0 Then Me.SetItemCheckState(i, CheckState.Checked) Else Me.SetItemCheckState(i, CheckState.Unchecked) End If poweredNumber *= 2 Next Catch ex1 As ArgumentException Throw ex1 Catch ex As Exception Throw ex End Try End Set End Property
''' <summary> ''' Gets or sets the data source for this CustomControls.CheckedListBox. ''' ''' Exceptions: ''' System.ArgumentException: ''' The assigned value does not implement the System.Collections.IList or ''' System.ComponentModel.IListSource intefaces. ''' </summary> ''' <returns> ''' An object that implements the System.Collections.IList or ''' System.ComponentModel.IListSource interfaces, such as a ''' System.Data.DataSet or an System.Array. The default is null. ''' </returns> <DefaultValue(""), AttributeProvider(GetType(IListSource)), RefreshProperties(RefreshProperties.All), Browsable(True)> _ Public Shadows Property DataSource() As Object Get Return MyBase.DataSource End Get Set(ByVal value As Object) MyBase.DataSource = value End Set End Property
''' <summary> ''' Gets or sets the property to display for this ''' CustomControls.CheckedListBox ''' </summary> ''' <returns> ''' A System.String specifying the name of the object property that is ''' contained in the collection specified by the ''' CustomControls.CheckedListBox.DataSource property. The default is ''' an empty string (""). ''' </returns> <DefaultValue(""), _ TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), _ Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", GetType(UITypeEditor)), _ Browsable(True)> _ Public Shadows Property DisplayMember() As String Get Return MyBase.DisplayMember End Get Set(ByVal value As String) MyBase.DisplayMember = value End Set End Property
<DefaultValue(""), _ TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), _ Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", GetType(UITypeEditor)), _ Browsable(True)> _ Public Shadows Property ValueMember() As String Get Return MyBase.ValueMember End Get Set(ByVal value As String) MyBase.ValueMember = value End Set End Property
<Browsable(False)> _ Public ReadOnly Property SelectedValues() As Array Get Dim i As Integer = -1 Dim ret() As Object = Nothing For Each idx As Integer In Me.SelectedIndices i += 1 ReDim Preserve ret(i) ret(i) = CType(MyBase.DataManager.List.Item(idx), DataRowView).Item(Me.ValueMember) Next Return ret End Get End Property
<Browsable(False)> _ Public ReadOnly Property CheckedValues() As Array Get Dim i As Integer = -1 Dim ret() As Object = Nothing For Each idx As Integer In Me.CheckedIndices i += 1 ReDim Preserve ret(i) ret(i) = CType(MyBase.DataManager.List.Item(idx), DataRowView).Item(Me.ValueMember) Next Return ret End Get End Property
End Class
Thanks again!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Thanks for this vb sample of a databound CheckedListBox, and thanks to Hossein eraghi for the initial post.
I have taken the code from the vb conversion and created my own ExtendedCheckedListbox control. However whenI have finished populating it the ValueMember stored the correct value (a unique integer for the datarow) but the DisplayMember doesn't and instead of the text for the list items I get "System.Data.DataRowView" for each item?
I have created a textbox to get the Value and Text of the SelectedItem and while the value changes the text does not.
Do you have any idea whats going on, because I don't?
Dim strSQL As String strSQL = "SELECT tccRoles.RoleID, tccRoles.RoleName FROM tccSiteRoles WHERE tccRoles.SiteID = 1" Using oSQLConn As New SqlConnection(ConfigurationManager.ConnectionStrings("tccDataStore").ConnectionString) Using oDA As New SqlDataAdapter(strSQL, oSQLConn) oDA.Fill(oDSet, "SiteRoles") End Using End Using ExchkListRoles.ValueMember = oDSet.Tables("SiteRoles").Columns("RoleID").ColumnName ExchkListRoles.DisplayMember = oDSet.Tables("SiteRoles").Columns("RoleName").ColumnName ExchkListRoles.DataSource = oDSet.Tables("SiteRoles")
Karl
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
 |
Hello, try this: for (int i = 0; i < chkBoxListCategory.Items.Count; i++) { foreach (DataRow dtrCategory in ocomdrop.DTCategory.Rows) { string x = chkBoxListCategory.GetItemText(chkBoxListCategory.Items[i]); bool y = chkBoxListCategory.GetItemChecked(i); if (dtrCategory["CategoryName"].ToString() == x & y) { MessageBox.Show(y.ToString()); } } }
Bye, Christopher.
-- modified at 16:18 Wednesday 29th November, 2006
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
 |
Hi thanks for a nice control.
I was looking for a control just like that but after a while I realized that I was missing something.
If I have the different items that the control should display in a table like this.
Sample table named BitFieldValues
SortOrder DisplayText BitValue --------------------------------- 1 Car 1 2 Truck 2 3 Bus 4
// Example sql statement retrieving the data from the table // I have left out the code reading from the database because that’s not the problem.
sql statment-> Select * Form BitFieldValues Order By SortOrder // Adds the DisplayText to the control. this.exCheckedListBox1.Items.Add("Car"); this.exCheckedListBox1.Items.Add("Truck"); this.exCheckedListBox1.Items.Add("Bus");
// This would render the following bit values // Car = 1 // Truck = 2 // Bus = 4
What would happen if someone wants to change the query that retrieves the information, let’s say that the items should be sorted alphabetic instead.
Select * Form BitFieldValues Order By DisplayText // Adds the DisplayText to the control. this.exCheckedListBox1.Items.Add("Bus"); this.exCheckedListBox1.Items.Add("Car"); this.exCheckedListBox1.Items.Add("Truck");
// This would render the following bit values // Car = 2 // Truck = 4 // Bus = 1
The bit fields values have changed and any information previous stored in a database would display the wrong information. The same would happened if I delete or set a flag oin the table that a specific row shouldn't display in the control.
I hope I have explained my problem so you can understand it.
Best regards Bjorn
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
My understanding is that the aggregated bit value of the selected items is calculated by the control itself and doesn't require the individual values to be stored anywhere. The order of the items as they appear in the listbox determines an individual item's bit value eg 1, 2, 4, 8, etc
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |