[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

checkedlistbox

Posted on 2012-08-23
2
Medium Priority
?
1,100 Views
Last Modified: 2012-08-27
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?
0
Comment
Question by:r3nder
2 Comments
 
LVL 16

Accepted Solution

by:
Rose Babu earned 2000 total points
ID: 38329483
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
 
LVL 6

Author Closing Comment

by:r3nder
ID: 38338917
Worked great srosebabu! Thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Loops Section Overview

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question