Click here to Skip to main content
15,881,882 members
Articles / Desktop Programming / Windows Forms
Article

Databinding CheckListBoxControl with DataView

Rate me:
Please Sign up or sign in to vote.
2.47/5 (9 votes)
6 Mar 2005 63.8K   24   3
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

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

to get the checked value.

C#
[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


Written By
Web Developer
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 2 Pin
ragav_pugal@yahoo.com16-Feb-10 22:30
ragav_pugal@yahoo.com16-Feb-10 22:30 

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

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