Click here to Skip to main content
Click here to Skip to main content

Databinding CheckListBoxControl with DataView

, 6 Mar 2005
Rate this:
Please Sign up or sign in to vote.
Databinding CheckListBoxControl with DataView.

Introduction

After a long day of struggle to get DevExpress XtraGrid working with an unbound CheckBox colum, I eventually succeeded, but at the cost of UI consistency. So I had to go for another option... CheckedListBoxControl.

Using .NET standard CheckedListBoxControl, I couldn't find the properties DataSource, DisplayMember, and ValueMember. Probably the reason would be an older .NET Framework. So instead of getting into the hassle, I decided to use the DevExpress CheckedListBoxControl.

Below is the code for my generic methods I have used for the CheckedListBoxControl. I would like to point here that, I have used a DataView as a datasource for my CheckedListBoxControl and not simple items. So the .Items property is not of any use here but .CheckedItems, .CheckedIndices, and .ItemCount properties are used. .CheckedItems returns all the checkbox items as an object, while .CheckedIndices returns indexes of all the .CheckState = "Checked" items. I have used these indices as the rowIndex of the DataView to fetch the values from the DataView. So it is always better to use

foreach (int index in chkList.CheckedIndices)
 { ... = myDataRow[index].Row["FIELDNAME"].ToString(); ... }

to get the checked value.

[C#]
#region CheckBox Generic Methods
public void SelectAll(DevExpress.XtraEditors.CheckedListBoxControl chkList) { 
    for(int i=0;i< chkList.ItemCount;i++) {
      chkList.SetItemChecked(i, true);
    }             
}
public void RemoveAll(DevExpress.XtraEditors.CheckedListBoxControl chkList) { 
    for(int i=0;i< chkList.ItemCount;i++) {
      chkList.SetItemChecked(i, false);  
    }       
}
public bool Checked(DataView dvSource, 
      DevExpress.XtraEditors.CheckedListBoxControl chkList, 
      string keyID, string fieldName) {
  bool stat = false;
  try{
    for (int i=0; i< dvSource.Count; i++ ) {
      if (dvSource[i].Row[fieldName].ToString()  == keyID) {
        chkList.SetItemChecked (i, true);
        stat = true;
        break;
      }
    }
  }
  catch(Exception ex){
    Code.ShowMessage(ex.ToString());
 }
 return stat;
}
public bool UnChecked(DataView dvSource, 
     DevExpress.XtraEditors.CheckedListBoxControl chkList, 
     string keyID, string fieldName) {
  bool stat = false;
  try{                      
    for (int i=0; i< dvSource.Count; i++ ) {
      if (dvSource[i].Row[fieldName].ToString() == keyID.ToString()) {
        chkList.SetItemChecked (i, false);
        stat = true;
        break;
      }
    }     
  }
  catch(Exception ex){
    Code.ShowMessage(ex.ToString());
  }
  return stat;
}
#endregion

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Syed Irtaza Ali
Web Developer
Pakistan Pakistan
No Biography provided

Comments and Discussions

 
GeneralMy vote of 2 Pinmemberragav_pugal@yahoo.com16-Feb-10 22:30 
GeneralSimple but easy to understand Pinmemberanhnt_fvl15-Jun-08 16:00 
Thanks for you effort!
GeneralMore info PinmemberGh0stman6-Nov-06 6:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 7 Mar 2005
Article Copyright 2005 by Syed Irtaza Ali
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid