checkedlistbox

I have a dataset I am binding to a Checkedlistbox - I am triying to set the checkbox to checked if it is a 1 in the dataset - how can I do  this

         protected void LoadRoles(int userid)
         {

             string connectionString = ConfigurationManager.ConnectionStrings["TaskManager"].ToString();
             SqlConnection con = new SqlConnection(connectionString);
             try
             {
                 int iuserid = userUIDValue;// Selected agent UID
                 SqlCommand com = new SqlCommand("sp_Membership_GetAllRoleswithUser", con);
                 com.CommandType = CommandType.StoredProcedure;
                 com.Parameters.AddWithValue("@useruid", iuserid);
                 SqlDataAdapter sqldba = new SqlDataAdapter(com);
                 DataSet dsRoles = new DataSet();
                 sqldba.Fill(dsRoles);
                 dsRoles.Tables[0].DefaultView.RowFilter = "InRole = 1";
                 rlbActiveRoles.DataSource = dsRoles.Tables[0];
                 rlbActiveRoles.DisplayMember = "RoleDefinition";
                 rlbActiveRoles.ValueMember = "roledefuid";
                // rlbActiveRoles = "roles";
                 
                // rlbActiveRoles. = "roles";
             }
             catch (Exception exLoadRolesandGroups)
             {
                 con.Close();
                 MessageBox.Show(exLoadRolesandGroups.Message.ToString());
             }
             try
             {
                 int iuserid = userUIDValue;// Selected agent UID
                 SqlCommand com = new SqlCommand("sp_Membership_GetAllRoleswithUser", con);
                 com.CommandType = CommandType.StoredProcedure;
                 com.Parameters.AddWithValue("@useruid", iuserid);
                 SqlDataAdapter sqldba = new SqlDataAdapter(com);
                 DataSet dsRoles = new DataSet();
                 sqldba.Fill(dsRoles);
                 dsRoles.Tables[0].DefaultView.RowFilter = "InRole = 0";
                 rlbRoles.DataSource = dsRoles.Tables[0];
                 rlbRoles.DisplayMember = "RoleDefinition";
                 rlbRoles.ValueMember = "roledefuid";
             }
             catch (Exception exLoadRolesandGroups)
             {
                 con.Close();
                 MessageBox.Show(exLoadRolesandGroups.Message.ToString());
             }
         }

Open in new window

Does anyone know how to do this?
LVL 6
r3nderAsked:
Who is Participating?
 
Rose BabuSenior Team ManagerCommented:
Hi,

Here is a sample code to set the checkbox status ina checkedListBox control.

i simple added checkedListBox control (checkedListBox1) into the form (frmCommon) and added the code below.

i pulled the data from the xml file (XMLTempFile.xml).

//	frmCommon.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WinAppCSHARP
{
    public partial class frmCommon : Form
    {
        public frmCommon()
        {
            InitializeComponent();
        }

        DataTable dtList = new DataTable();

        private void frmCommon_Load(object sender, EventArgs e)
        {
            BindCheckedListBox();
        }

        public void BindCheckedListBox()
        {
            DataSet ds = new DataSet();
			
			// XMLTempFile.xml will be placed in bin/debug folder
            ds.ReadXml(Application.StartupPath + "/XMLTempFile.xml");

            dtList = ds.Tables[0];

            checkedListBox1.DataSource = dtList;

            checkedListBox1.DisplayMember = "Name";
            checkedListBox1.ValueMember = "Checked";

            setCheckBoxState();
        }

        public void setCheckBoxState()
        {
            int listCount = checkedListBox1.Items.Count;

            for (int i = 0; i < listCount; i++)
            {
                if (dtList.Rows[i]["Checked"].ToString() == "1")
                {
                    checkedListBox1.SetItemChecked(i, true);
                }
            }
        }
    }
}

Open in new window


//	XMLTempFile.xml

<?xml version="1.0" encoding="utf-8" ?>
<items>
  <item>
    <Name>ADAMS, Michael</Name>
    <Checked>0</Checked>
  </item>
  <item>
    <Name>ADDINGTON, J.</Name>
    <Checked>0</Checked>
  </item>
  <item>
    <Name>AJAYI, Michael</Name>
    <Checked>1</Checked>
  </item>
  <item>
    <Name>ARACRI, Andy</Name>
    <Checked>1</Checked>
  </item>
  <item>
    <Name>ARMSTRONG, B.</Name>
    <Checked>1</Checked>
  </item>
  <item>
    <Name>BARZ, Mike</Name>
    <Checked>1</Checked>
  </item>
  <item>
    <Name>BATH, Mike</Name>
    <Checked>1</Checked>
  </item>
  <item>
    <Name>BELL, Jacob</Name>
    <Checked>0</Checked>
  </item>
  <item>
    <Name>BENDER, Lamont</Name>
    <Checked>1</Checked>
  </item>
  <item>
    <Name>BENEKER, Alan</Name>
    <Checked>1</Checked>
  </item>
  <item>
    <Name>BENEKER, Allen</Name>
    <Checked>1</Checked>
  </item>
  <item>
    <Name>BENNETT, Jerome</Name>
    <Checked>0</Checked>
  </item>
  <item>
    <Name>BETTS, Josh</Name>
    <Checked>1</Checked>
  </item>
  <item>
    <Name>BILINS, Brett</Name>
    <Checked>1</Checked>
  </item>
  <item>
    <Name>BLACKMON, C.</Name>
    <Checked>0</Checked>
  </item>
  <item>
    <Name>BOWEN, Milt</Name>
    <Checked>1</Checked>
  </item>
  <item>
    <Name>BRANCH, Jason</Name>
    <Checked>1</Checked>
  </item>
  <item>
    <Name>BRANDT, Matt</Name>
    <Checked>0</Checked>
  </item>
  <item>
    <Name>BRAUN, Nick</Name>
    <Checked>1</Checked>
  </item>
  <item>
    <Name>BRINCK, Josh</Name>
    <Checked>1</Checked>
  </item>
  <item>
    <Name>BRITTAIN, Ryan</Name>
    <Checked>1</Checked>
  </item>
  <item>
    <Name>BROWN, Josh</Name>
    <Checked>0</Checked>
  </item>
  <item>
    <Name>BRUMBERGS, Andy</Name>
    <Checked>0</Checked>
  </item>
  <item>
    <Name>BUNCE, Nate</Name>
    <Checked>0</Checked>
  </item>
  <item>
    <Name>BURKE, Steve</Name>
    <Checked>0</Checked>
  </item>
  <item>
    <Name>BURT, Larry</Name>
    <Checked>1</Checked>
  </item>
  <item>
    <Name>BUSING, John</Name>
    <Checked>1</Checked>
  </item>
  <item>
    <Name>BUSING, Ryan</Name>
    <Checked>1</Checked>
  </item>
  <item>
    <Name>BUXTON, Ty</Name>
    <Checked>1</Checked>
  </item>
  <item>
    <Name>CALHOUN, Jimmy</Name>
    <Checked>1</Checked>
  </item>
  <item>
    <Name>CAPPER, Andy</Name>
    <Checked>1</Checked>
  </item>
  <item>
    <Name>CARD, Joey</Name>
    <Checked>0</Checked>
  </item>
  <item>
    <Name>CASE, James</Name>
    <Checked>1</Checked>
  </item>
  <item>
    <Name>CIBOROWSKI, A.</Name>
    <Checked>1</Checked>
  </item>
  <item>
    <Name>CIBOROWSKI, Ada</Name>
    <Checked>1</Checked>
  </item>
</items>

Open in new window

This will clear you on how to change the checkedListBox's checked state.

Hope this will help you on your process.
0
 
r3nderAuthor Commented:
Worked great srosebabu! Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.