Had all sorts of problems trying to get the Winforms CheckedListBox to data bind properly. Found this article and code sample that helped:
http://www.codeproject.com/cs/combobox/ExCheckedListBox.asp
Just needed to make a couple of changes:
1. Use the ValueMember property rather than the index:
[Bindable(true), Browsable(true)]
public int Value
{
get
{
///Gets checked items in decimal mode from binary mode
try
{
//loop in all items of list
for (int i = 0; i < this.ItemCount; i++)
{
if (this.GetItemChecked(i))
{
DataRowView drv = (DataRowView)this.GetItem(i);
int _value = (int)drv[this.ValueMember];
this.value |= (int) Math.Pow(2, _value - 1);
}
}
}
catch (ArgumentException ex)
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
return this.value;
}
set
{
///sets checked items from binary mode converted from decimal value
this.value = value;
try
{
for (int i = 0; i < this.ItemCount; i++)
{
DataRowView drv = (DataRowView)this.GetItem(i);
int _value = (int)drv[this.ValueMember];
int checkValue = (int)Math.Pow(2, _value - 1);
if ((value & checkValue) == checkValue)
this.SetItemCheckState(i, CheckState.Checked);
//else remove checked from item
else
this.SetItemCheckState(i, CheckState.Unchecked);
}
}
catch (ArgumentException ex)
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
}
}
2. add a custom function in T-SQL for the reports:
--select * from fnGetCites(4)
ALTER FUNCTION fnGetCites
(
@Code INT
)
RETURNS @ReturnTable TABLE (CityID INT, CityName VARCHAR(50))
AS BEGIN
DECLARE @CityID INT
DECLARE @CityName VARCHAR(50)
--loop in all items in the cites table
DECLARE crsr CURSOR FOR SELECT CityID,
CityName
FROM Cities
OPEN crsr
FETCH NEXT FROM crsr INTO @CityID, @CityName
WHILE @@FETCH_STATUS = 0 BEGIN
IF (@Code & POWER(2, @CityID- 1)) = POWER(2, @CityID - 1)
--ADD TO return table
INSERT INTO @ReturnTable
VALUES(@CityID, @CityName)
FETCH NEXT FROM crsr INTO @CityID, @CityName
END
CLOSE crsr
DEALLOCATE crsr
RETURN
END
posted on Monday, October 02, 2006 6:58 PM