Monday, May 21, 2007

CheckBox List in C#.NET (Windows applications)

Some of the Properties to be modifed:

Behaviour: CheckonClick = true //If this is set to false you need to click twice to check an item.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Binding to a CheckBox List

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

DataSet dsRegionGroups = new ForecastGen.Data.RegionGroups().SelectAllRegionGroups(); //This retrieves the data from DataBase

chkLstRegionGroups.DataSource = dsRegionGroups.Tables[0];

chkLstRegionGroups.ValueMember = "RegionGroupMstId";

chkLstRegionGroups.DisplayMember = "RegionGroupName";

//Please make sure the order of statements is correct.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

To Check/UnCheck an item programmatically

for (Int16 i = 0; i < chkLstRegionGroups.Items.Count; i++)

{

chkLstRegionGroups.SetItemChecked(i, false); //uncheck

}

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Retrieving the Selected Values Programmatically

foreach (DataRowView view in chkLstRegionGroups.CheckedItems)

{

dr = dt.NewRow();

dr[0] = userid;

dr[1] = view[chkLstRegionGroups.ValueMember].ToString();

dt.Rows.Add(dr);

}

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

 

 

1 comment:

Bob said...

Thank you for this. I have been trying to figure out how to uncheck the items in a checkboxlist whenever the user clicks on a Reset button control that I put on my form. There are so many properties to the checkboxlist related to each item in the list being "checked" or "selected" and I have found it difficult to understand which to use. This worked for me. Thanks again.