CheckList实例

仅显示选中项,没有被选中的不显示 

cblDeptMent.Items.Clear();

 

        SqlConnection conn = new SqlConnection(myConnectionString);
        SqlCommand command = new SqlCommand(" select Permissionid from dbo.tbl_User_UserPermission where iUser_no = @IUserNo", conn);//用户对应权限表
        SqlParameter parameter1 = new SqlParameter("@IUserNo", SqlDbType.Int, 4);
        parameter1.Value = iUserNo;
        command.Parameters.Add(parameter1);
        command.Connection.Open();
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = command;
        DataSet ds = new DataSet();
        adapter.Fill(ds, "ds");

        SqlCommand commandUser = new SqlCommand(" select permissionid,description from dbo.tbl_User_Permission where cSysNo=02", conn);//tbl_User_Permission 全部权限表
        SqlDataAdapter adapterUser = new SqlDataAdapter();
        adapterUser.SelectCommand = commandUser;
        DataSet dsuser = new DataSet();
        adapterUser.Fill(dsuser, "dtuser");
        DataTable dtuser = dsuser.Tables[0];


        DataTable dt = ds.Tables[0];
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            for (int j = 0; j < dtuser.Rows.Count; j++)
            {
                if (dt.Rows[i]["Permissionid"].ToString() == dtuser.Rows[j]["permissionid"].ToString())
                {
                    ListItem listitem = new ListItem(dtuser.Rows[j]["description"].ToString());//比较用户所对应的权限在权限表中是否存在
                    cblDeptMent.Items.Add(listitem);
                    cblDeptMent.Items[i].Selected = false;
                }
            }
        }

你可能感兴趣的:(check)